如何在选中默认设置的情况下显示 Remember_me

发布于 2024-11-19 07:45:24 字数 877 浏览 2 评论 0原文

对于设计注册表:

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一百个冬季 2024-11-26 07:45:24

首先检查是否启用了 Devise Rememberable,然后在 Rails 中添加带有标准表单助手的复选框。为了使其默认启用,我们传递 :checked =>;在选项哈希中“检查”:

<% if devise_mapping.rememberable? -%>
    <%= f.check_box :remember_me, {:checked => "checked"} %> 
    <%= f.label :remember_me %>
<% end -%>

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:

<% if devise_mapping.rememberable? -%>
    <%= f.check_box :remember_me, {:checked => "checked"} %> 
    <%= f.label :remember_me %>
<% end -%>
遇见了你 2024-11-26 07:45:24

最近有人向我指出,上面建议的方法(确实有效)并不理想,因为用户可以取消选中“记住我”,但他们的凭据会出错。当表单重新加载时,“记住我”将再次被检查,他们可能不会注意到。

所以你可以使用类似的东西:

<%= f.check_box :remember_me, (resource.remember_me ? {} : { checked: true }) %>

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:

<%= f.check_box :remember_me, (resource.remember_me ? {} : { checked: true }) %>
乙白 2024-11-26 07:45:24

首先,看一下 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文