如何在选中默认设置的情况下显示 Remember_me
对于设计注册表:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up", :class => 'button' %></p>
<% end %>
我如何添加一个 Remember_me 复选框,上面写着“让我在这台计算机上保持登录状态”。
另外,如何检查默认设置?
我尝试过此操作,但在页面加载时从未选中该复选框。
<%= f.check_box :remember_me %>
<%= f.label :remember_me, 'Keep me logged-in on this computer.', :style => 'display: inline-block;' %>
谢谢
for the devise registration form:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up", :class => 'button' %></p>
<% end %>
How can I add a remember_me checkbox, that says something like "Keep me logged-in on this computer."
Also, how can I make the default setting checked?
I tried with this but the checkbox is never checked on page load.
<%= f.check_box :remember_me %>
<%= f.label :remember_me, 'Keep me logged-in on this computer.', :style => 'display: inline-block;' %>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先检查是否启用了 Devise Rememberable,然后在 Rails 中添加带有标准表单助手的复选框。为了使其默认启用,我们传递 :checked =>;在选项哈希中“检查”:
First check that Devise rememberable is enabled and then add the checkbox with the standard form helper in Rails. To make it enabled by default we pass :checked => "checked" in the options hash:
最近有人向我指出,上面建议的方法(确实有效)并不理想,因为用户可以取消选中“记住我”,但他们的凭据会出错。当表单重新加载时,“记住我”将再次被检查,他们可能不会注意到。
所以你可以使用类似的东西:
Someone pointed out to me recently that the suggested approach above (which does work) is not ideal because a user could un-check 'remember me' but make a mistake with their credentials. When the form re-loads 'remember me' will be checked again and they may not notice.
So you could instead use something like:
首先,看一下 api
http://api .rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
这将是找到此类问题答案的地方。
接受的参数之一是选项哈希,我相信如果您按照
:checked => 的方式传递一些内容, true
它将实现您正在寻找的内容。First, take a look at the api
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
That will be the place to find answers to questions like this.
one of parameters accepted is an options hash, I believe if you pass in something along the lines of
:checked => true
it will accomplish what you're looking for.检查这个Rails 应用程序中“记住我”的实现 。
check this Implementation of "Remember me" in a Rails application .