简体   繁体   English

使check_box在Rails表单中工作

[英]Getting check_box to work in a Rails form

My objective is to use a check box on the sign-in form to allow the user to choose to stay signed in after browser close. 我的目标是使用登录表单上的复选框,以允许用户选择在浏览器关闭后保持登录状态。 I'm using this code in the sign-in form for the check box (views > sessions > new.html.erb): 我在登录表单的复选框中使用了以下代码(视图>会话> new.html.erb):

<div class="field">
  <%= f.check_box :stay_signed_in %> Stay signed in?
</div>

:stay_signed_in is therefore 1 if checked, and 0 if not. 因此::stay_signed_in如果为1,则为1,否则为0。 I then (attempt to) set a session variable :staysignedin to either true or false depending on the value of :stay_signed_in (in the sessions controller): 然后,我(尝试)根据:stay_signed_in的值(在会话控制器中)将会话变量:staysignedin设置为true或false:

def create
  session[:staysignedin] = (params[:session][:stay_signed_in] == "1") ? true : false
  ...
end

There is something wrong with this code. 此代码有问题。 Even when the box is checked and :stay_signed_in is definitely 1, session[:staysignedin] is never set to true. 即使选中此框,并且:stay_signed_in绝对为1,session [:staysignedin]也永远不会设置为true。 Where am I going wrong? 我要去哪里错了?

Edit: The rest of the sign-in form looks like this: 编辑:登录表单的其余部分如下所示:

<%= form_for(:session, :url => sessions_path) do |f| %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>

  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>

  <div class="field">
    <%= f.check_box :stay_signed_in %>
    Stay signed in on this computer?
  </div>

  <div class="actions">
    <%= f.submit "Sign in" %>
  </div>

<% end %>

Edit: correction: it IS setting session[:staysignedin] to be true or false correctly, so the fault must be elsewhere. 编辑:纠正:它正在将session [:staysignedin]正确设置为true或false,因此故障必须在其他位置。 The problem is that with 'stay signed in' checked, when I close the browser and reopen it, the user is not still signed in. When I remove the 'stay signed in?' 问题在于,选中“保持登录状态”后,当我关闭浏览器并重新打开浏览器时,用户仍未登录。当我删除“保持登录状态吗?” check box on the sign in form and all the relevant code, leaving only the permanent sign-in code, it works fine. 登录表单上的复选框和所有相关代码,仅保留永久登录代码,即可正常工作。

def sign_in(user)
  if session[:staysignedin]
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  else
    session[:userid] = user.id
  end
  self.current_user = user
end

def sign_out
  if session[:staysignedin]
    cookies.delete(:remember_token)
  else
    session[:userid] = nil
    session[:staysignedin] = nil
  end
  self.current_user = nil
end

What does the rest of your form code look like? 其余的表单代码是什么样的?

My guess is that :stay_signed_in is not storing to params[:session][:stay_signed_in] , but rather params[:something_else][:stay_signed_in] or just params[:stay_signed_in] 我的猜测是:stay_signed_in不是存储到params[:session][:stay_signed_in] ,而是params[:something_else][:stay_signed_in]或只是params[:stay_signed_in]

If you're running on local WEBrick you can see the parameters that are posted with each request, that will help you make sure you've got the right parameter key. 如果您在本地WEBrick上运行,则可以看到每个请求发布的参数,这将有助于确保您具有正确的参数密钥。

-EDIT- -编辑-

Based on your last comment, I would recommend using Devise to handle your authentication logic. 根据您的最新评论,我建议您使用Devise处理身份验证逻辑。 It is super-easy to implement, fully Rails 3 compatible, and includes built in code for handling sessions and remembering to stay logged in. 它实现起来非常容易,与Rails 3完全兼容,并且包括用于处理会话和记住保持登录状态的内置代码。

It's probably not worth your time to get stuck for this long on a simple sessions remembering issue when there's such a good plugin available that is lightweight, rock-solid, well-documented etc. 当有一个如此好的插件可用时,例如轻量级的,坚如磐石的,有据可查的等等,在一个简单的会话记忆问题上停留这么长时间可能不值得您花时间。

For an intro, see this Railscast . 有关介绍,请参阅此Railscast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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