how to move to your desired page after DEVISE AUTHENTICATION in Rails - ruby-on-rails

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

Related

Couldn't make changes in users/session/new file in rails used with devise

I installed devise in my application and signup, signin and all the actions are working fine.
I just replaced my heading from "Log in" to "sign in", it is not affected in the application.
Please help me out.
This is the command i run after installing devise
$ rails generate devise User
users/sessions/new:
Sign in
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "users/shared/links" %>
routes.rb:
devise_for :users
And i haven't created any controllers for devise.
In the file config/initializers/devise.rb, make
# config.scoped_views = false to
config.scoped_views = true

Registration Form: Unable to Hide Field

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

Sign In/Up Form in different migrations

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

Updating a user model attribute through a Devise session login

I am using the Devise authentication gem for a Ruby on Rails app. When a user logs in by creating a new session I would like to update a column in my user model. What would be the best way to do this?
Is there any way to have a hidden field that updates the model?
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></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 -%>
<% f.hidden_field :field_a, :value => 'test' %>
<div><%= f.submit "Sign in" %></div>
<% end %>
You can do this by overwriting Devise::SessionsController create method. When the user login it will just authenticate the user, it wont update user record. So there is no use of keeping the hidden field in the form
Try like this
class Users::SessionsController < Devise::SessionsController
def create
#do your update here
end
end
or else you can follow this link http://denmarkin.tumblr.com/post/5194645960/how-to-overwrite-devise-sessions-controller-in-rails-3

How to show a remember_me with the default setting as checked

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 .

Resources