全部,
我正在处理登录表单。作为该表格的一部分,我加入了一个"记住我"复选框,用于在选中时在用户数据库中存储加密的会话哈希。我可能在基本方面遗漏了一些东西,但对于我的生活,我无法让复选框正常运行。具体来说,每次提交登录都会导致会话哈希存储到数据库中,即使未单击该复选框,也会导致我认为该复选框没有正确设置。
告诉我我的方式错误!
登录视图:
.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
答案 0 :(得分:1)
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
remember user
我不太确定你有第二个“记住用户”,这可能是导致你遇到的问题。