0
您好我想創建一個Ruby on Rails身份驗證。 我沒有錯誤 - 消息,但我不能登錄。Ruby - 身份驗證..登錄問題
在我的本地這張照片出現(對不起,我不能發表屏幕)
我Workingtimes
Ç2011 |登錄
登錄按鈕不起作用。
這裏是我的代碼:
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Mytimes</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<div id="header">
<h1> My Workingtimes </h1>
</div>
<div id="content">
<% if flash[:notice] %>
<p id="notice">
<%= flash[:notice] %>
</p>
<% end %>
<% if flash[:alert] %>
<p id= "alert">
<%= flash[:alert] %>
</p>
<% end %>
</div>
<div id="footer">
© 2012 |
<% if user_signed_in? %>
<%= link_to "logout", logout_path, method: :delete %>
<% else %>
<%= link_to "login", login_path %>
<% end %>
</div>
</div>
</body>
</html>
的routes.rb
Mytimes::Application.routes.draw do
resources :mytimes
resources :users, :only => [:new, :create]
resources :sessions, :only => [:create]
get "login" => "sessions#new", as: "login"
post "sessions" => "sessions#create", as: "sessions"
delete "logout" => "sessions#destroy", as: "logout"
application_controller.rb
類ApplicationController中<的ActionController ::基地 protect_from_forgery
private
def current_user
if session[:user_id]
@current_user ||= User.find(session[:user_id])
end
end
def user_signed_in?
current_user.present?
end
helper_method :user_signed_in?
end
session_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to mytimes_path,
notice: "Your are signed!"
else
flash.now.alert = "Failed Email or Password!"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to mytimes_path,
notice: "You are logged out!"
end
end
會話/ new.html.erb
<h2> Log In </h2>
<%= form_tag sessions_path do %>
<p>
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</p>
<p>
<%= label_tag :password %>
<%= password_field_tag :password %>
</p>
<p><%= submit_tag "Log In" %></p>
<% end %>
蔭噸盼望你的幫助。
用戶/ new.html.erb
<h2>New User</h2>
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</p>
<p><%= f.submit %></p>
<% end %>
謝謝!有用。 – UserS