开发者

How to show a remember_me with the default setting as checked

开发者 https://www.devze.com 2023-03-18 04:32 出处:网络
for the devise registration开发者_高级运维 form: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

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


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 -%>


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 }) %>


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.


check this Implementation of "Remember me" in a Rails application .

0

精彩评论

暂无评论...
验证码 换一张
取 消