我正在使用登錄表單。作爲該表格的一部分,我添加了「記住我」複選框,用於在用戶數據庫中存儲加密的會話哈希值(如果選中)。我可能錯過了一些基本的方面,但對於我的生活,我無法讓複選框正常工作。具體來說,即使未點擊複選框,每次提交登錄都會導致會話哈希存儲到數據庫中,導致我相信複選框的設置不正確。
讓我看看我的方式錯誤!
登錄視圖:
.container
%h1 Log in
%div{:class => "row"}
%div{:class => "col-md-6 col-md-offset-3"}
= form_for :session, :url => login_path, :class => "form-horizontal", :id => "login_form" do |f|
%div{:class => "form-group"}
= f.label :email, "Email Address"
= f.text_field :email, :class => "form-control", :id => "email", :placeholder => "Email"
%div{:class => "form-group"}
= f.label :password, "Password"
= f.password_field :password, :class => "form-control", :id => "password", :placeholder => "Password"
%div{:class => "form-group"}
= f.label :remember_me, "Remeber me"
= f.check_box :remember_me, :type => "checkbox", :value => "1", :checked => "1"
= f.submit "Go Get Em!", :class => "btn btn-default pull-right"
%p Don't have an account? #{ link_to 'Sign Up', signup_path}
而且會話控制器的方法:
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in(user)
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
remember user
redirect_to root_url #redirect_to user
else
flash.now[:danger] = "Invalid email/password comination" #fix
render 'new'
end
end
您是否使用設計進行身份驗證? – penner
不,我正在使用bcrypt。 – iammurp