So I have been using Authlogic to allow user account creation and authentication. I have most of the features I want for the user but I am currently running into trouble regarding the "remember_me" function.
I have a check box to indicate whether the user wants to be remembered after the closed the browser or not. However, no matter what, even if I check the box or not, the remember me is set to false. When the user_session is created, I can see the value for "remember_me" being correct based on the state of the check box. Despite that, when I print out the value of "remember_me" to the console, it is false no matter whether the checkbox was checked or not.
The remember_me feature works as intended when I force the value to be true using the following:
@user_session.remember_me = true
and place that within the UserSession controller. The two examples I used both utilize check boxes to determine the value for the field and I believed I followed them correctly so I do not know why my check boxes are not setting the "remember_me" field appropriately. The one problem I think might be happening is that the check box is saving the value to another field called "remember_me" which is why the actual field is always false.
Does anyone know how to fix this issue or have any advice on how to do so? Thanks!
User Sessions Controller:
class UserSessionsController < ApplicationController
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(user_session_params)
#@user_session.remember_me = true
#puts(@user_session.remember_me)
if @user_session.save
flash[:success] = 'Welcome back'
redirect_to home_path
else
flash[:notice] = 'Failed to log in'
redirect_to :back
end
end
def destroy
current_user_session.destroy
flash[:success] = 'Goodbye'
#redirect_to root_path - Should redirect somewhere after signout
end
private
def user_session_params
params.require(:user_session).permit(:email,:password,:remember_me)
end
end
User Session "new.html.erb"
<div class="loginContainer" align="center">
<h1 align="center"><b>Log In</b></h1>
<%= form_for @user_session do |u| %>
<div class="container">
<%= render 'shared/errors', object:@user_session %>
<label><b>Email</b></label>
<%= u.text_field :email, placeholder: 'Email Address' %>
<!--<input type="text" placeholder="Enter Username" name="uname" required> -->
<br>
<label>
<b>Password</b>
</label>
<%= u.password_field :password, placeholder: 'Password' %>
<!--<input type="password" placeholder="Enter Password" name="psw" required> -->
<br>
<%= u.label 'Remember me' %>
<%= u.check_box :remember_me %>
<%= u.submit 'Login', class: 'btn btn-primary' %>
<!-- <button type="submit">Login</button> -->
</div>
<% end %>
<div class="container" style="background-color:#D3D3D3">
<%= link_to 'Forgot your password?', new_password_reset_path %>
</div>
</div>
User Session Model
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [ self.send(self.class.primary_key) ]
end
end
User Session Request Parameters (with checkbox checked):
<ActionController::Parameters {"email"=>"fake@gmail.com", "password"=>"password1", "remember_me"=>"1"} permitted: true>
Try printing out the exact data that is sent to your action.
puts params
=> {user_session: {..., remember_me: "1"}}
Then try the operation in console to see if the checkbox data is being interpreted as true
, I suspect this is the issue.
rails c
params = {..., remember_me: "1"}
session = UserSession.new(params)
session.remember_me?
=> false # if "1" is not setting this to true
=> true # if "1" is setting this true
If this is the issue you need the data to be a Ruby true
boolean.
def user_session_params
if params[:user_session][:remember_me].present? # not nil or ""
params[:user_session][:remember_me] = true
end
params.require(...).permit(...)
end
Though I have this feeling that Rails is smart enough to handle this boolean column as presence based.
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.