简体   繁体   中英

Rails, HAML remember me checkbox not working

All,

I am working on a login form. As part of that form, I have included a "Remember me" checkbox, designed to store an encrypted session hash in the user database if checked. I am probably missing something on the basic side, but for the life of me, I cannot get the checkbox to function properly. Specifically, every submission of the login results in a session hash being stored to the database, even if the checkbox is not clicked, leading me to believe that the checkbox simply is not set up correctly.

Show me the error of my ways!

the login view:

.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}

And the sessions controller method:

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
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
remember user

I'm not quite sure you have a second "remember user" in there, that's probably the issue that causes what you're experiencing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM