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 .
Related
I use devise_security_extension in order to have some password security features in my app.
I use password_expirable in one of my models, and when the password expires it goes to this view and I find no way to edit its content. Do you have any suggestion how I can control this view's HTML?
Currently, the gem will not automatically generate the view for you to edit. I think it's one of the things that people have asked for. In the meantime, you can manually create the file. If you look in the gem's:
devise_security_extension/app/views/devise/password_expired/show.html.erb
You'll see the current template for the view that the gem uses. Copy and paste this file into your views/devise/password_expired/show.html.erb
You'll then be able to edit it the way you'd like.
The file looks like this:
<h2>Renew your password</h2>
<%= form_for(resource, :as => resource_name, :url => [resource_name, :password_expired], :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :current_password, "Current password" %><br />
<%= f.password_field :current_password %></p>
<p><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Change my password" %></p>
<% end %>
In creating a new application I have used marklocklear example (https://github.com/marklocklear/devise_multi_tentant) for assistance in creating a multi-tenant environment. It works well.
The next thing I wanted to do I thought would be simple but has turned into quite the niggle for me.
All I wanted to do is rather than having the initial admin user have to enter an organization name (which is not necessary in my application), I wanted to hide that field.
Sign Up Form (that works):
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<%= f.fields_for :organization do |org| %>
<div><%= 'Organization or Company Name' %><br />
<%= org.text_field :name %></div>
<% end %>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
So, I did this to try to hide the field:
<%= f.fields_for :organization do |org| %>
<%= f.hidden_field :name %>
<% end %>
But I get an error.
The baffling thing to me is that if I leave the field in, but do not enter a value everything still works properly -- this is unexpected. Since (as part of my troubleshooting) I removed the field completely -- and got an error message stating the form could not be processed because the organization part was blank.
Any assistance would be appreciated.
Max: Thank you. As you know, that worked like a charm. The field is now hidden and the form submits properly. Thanks.
<%= f.fields_for :organization do |org| %>
<%= org.hidden_field :name %>
<% end %>
I am using Devise in my rails app to authenticate.
After signing in from sign in page it doesn't go to any other page. How do I configure Devise to move to my desired page after authenticating from sign-in page ?
Right now it is coming back to sign in page.
My sign in page is as follow:
<h2>Sign in ! Hussain</h2>
<h2><%= resource%></h2>
<h2><%= resource_name%></h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
<%= render "devise/shared/links" %>
You can do this by overriding the default methods of devise i.e. after_sign_in_path_for in application_controller. Check out Devise documentation. For your problem you can follow this link.
Hope that helps!!!
I have a devise users table with a fully functioning sign in/up form.
What I was wondering was how to have that users form appear on a different table.
For example
users/sign_in works perfectly
but
I want to have that form appear on movies/index
Ive tried adding the form code to the movies/index but i get this error
undefined local variable or method `resource' for #<#<Class:0x00000102cbf0b8>:0x00000103bb6d78>
This is the sign in form
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
<%= render "devise/shared/links" %>
Thanks!
You can generate the views (I understand you already did that) and you can override the controllers, or, in your case, you can watch the controllers of Devise, take the code that you need, and in your view call a partial (from the Devise views).
I did something like that a few months ago, but what I did (that I don't fully suggest but I haven't find a better way) was to take the code from the Devise views, and copy the code in another view with some modifications:
<%= form_for(User.new, :as => "user", :url => session_path("user"), :remote => true) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<div><%= f.submit "Sign in" , :class=>"blue_submit_degradiant", :id =>"sign_in_user"%></div>
<% end %>
It works, but the right way is to have a #user instead of User.new
<%-roles = Role.all%>
<%= panel "Edit" do%>
<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
<p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></p>
<%=f.input :roles,:collection => Role.all%>
<% end %>
<% f.buttons do %>
<%=f.submit "Update" %></p>
<% end %>
In here, when I don't type any password in, the error message shows but also the role of the user gets changed as well. How do I make it so that the role doesn't change?
The user's role changes are in memory, no in database. You can do model.reload to discard memory changes and reload your user from database if a validation error happens.