我是ruby on rails應用程序的新手,從最近幾天我患有Login
& Logout
問題,現在我已經達成了解決方案,但現在已經創建了一個新問題。當我給無效的密碼,然後它顯示Ruby on Rails登錄和註銷問題
無效的電子郵件/密碼組合
當我給有效的密碼,然後它顯示Logged In
。
但登錄邏輯後不工作。
這是我sessions_controller:
def new
end
def create
user = Jobseeker.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
redirect_to home_path
flash[:notice] = 'Logged In'
else
# Create an error message.
flash[:notice] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out
redirect_to home_path
end
這是我的模型:
before_save { email.downcase! }
validates :first_name, :presence => true, :length => { :in => 3..20 }
validates :last_name, :presence => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, format: { with: /\A[^@\s][email protected]([^@.\s]+\.)+[^@.\s]+\z/ }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
這是我的看法:
<%= form_for(:session, url: login_path) do |f| %>
<%= f.email_field :email %>
<%= f.password_field :password%>
<%= f.submit "Log in" %>
<% end %>
這是我sessions_helper:
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Returns the current logged-in user (if any).
def current_user
@current_user ||= Jobseeker.find_by(jobseeker_id: session[:user_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
# Logs out the current user.
def log_out
session.delete(:user_id)
@current_user = nil
end
這是我application_controller:
protect_from_forgery with: :exception
include SessionsHelper
這是我的佈局:
<% if logged_in? %>
<%= link_to "Profile", current_user.first_name %>
<%= link_to "Log out", logout_path, method: :delete %>
<% else %>
<li><%= link_to "Login", login_path %></li>
<% end %>
我怎樣才能解決這個問題?
我使用ruby 1.9.7
Rails 4.2.5
& mysql2
這將幫助我很多,請。
但是你在哪裏使用此方法登錄?控制器中沒有「登錄」用法,對吧?當你創建一個會話時,你不需要使用助手的方法。 – PatNowak