How to make Sign up page be root page in Devise?
devise_for :users
devise_scope :user do
authenticated :user do
root to: 'books#index'
end
unauthenticated :user do
root to: 'devise/registrations#new', as: :unauthenticated_root
end
end
How to set devise sign in page as root page in rails
Add this to your routes.rb
devise_scope :user do
root :to => 'devise/sessions#new'
end
but on doing this, after sign in you might get stuck in infinite loop error, so its better to add after sign in and after sign out path in your application controller by overriding the devise methods
def after_sign_in_path_for(resource_or_scope)
# your_path
end
def after_sign_out_path_for(resource_or_scope)
# your_path
end
This will work!
Setting Devise Login to be root page
To follow on from the people who are asking about the error Could not find devise mapping for path "/"
there is a workaround.
You'll find that there is a clue in your logs which will probably say:
[Devise] Could not find devise mapping for path "/".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
match "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
@request.env["devise.mapping"] = Devise.mappings[:user]
So I retried the approach but instead wrapping it (as @miccet suggets) inside a scope block:
devise_scope :user do
root to: "devise/sessions#new"
end
This worked fine for me
How to set rails devise sign_in path as a root url
look like you're trying to log in the same user again without a sign out
devise_for :admins, path: 'admins'
devise_scope :admin do
authenticated :admin do
root 'home#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
Rails 4:Error in setting up SignIn Page as root page using Devise
Try to set your route file like this:
devise_scope :user do
authenticated :user do
root 'home#dashboard', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
Devise provides some inbuilt methods like authenticated
, unauthenticated
etc. So as per your requirement, you can set different root
for logged in users and non-logged in users.:as
will create custom named url helpers like authenticated_root_path
and unauthenticated_root_path
If you will run rake routes
, you will get:
authenticated_root GET / home#dashboard
unauthenticated_root GET / devise/sessions#new
How to signup in two different pages with devise
You can achieve this by overriding the devise registration controller. In your overrided registration controller, in the new method once the user is saved,
#app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
if @user.save!
if @user.role == "librarian"
redirect_to librarian_profile_path
elsif @user.role == "student"
redirect_to student_profile_path
end
end
end
end
You can define librarian_profile_path
and student_profile_path
in your routes
#app/config/routes.rb
get 'librarian/new' => 'librarian_profile#new', :as => 'librarian_profile'
get 'student/new' => 'student_profile#new', :as => 'student_profile'
How to remain on the same page in case of unsuccessful sign up when using Devise? (Sign up form being displayed on root page)
me again.
I have solved the problem with a friend of mine after quite a bit of hustle.
Here goes a path to take that enables to disable automatic redirection to the devise form and enable to set your own and remain on the root_path after sign_up failure:
1) Add these lines to your routes in order to override the direct call-back to the Devise generated forms and to custom your user journey.
devise_for :users, controllers: {
registrations: 'users/registrations'
}
By doing so, you tell Rails that you are changing variables of the Devise Controller for Registrations.
2) In your controllers/users/registrations_controller.rb create a new method create.
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
render "pages/home"
end
end
What I did here was only to change the last parcel from:
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource, :location => after_sign_in_path_for(resource)
end
to
else
clean_up_passwords resource
set_minimum_password_length
render "pages/home"
end
3) Then you have to create a new file in your lib folder named custom_failure.rb (anywhere in the lib folder works perfectly fine). In this file, add the following lines:
class CustomFailure < Devise::FailureApp
def redirect_url
'/'
end
def route
'/'
end
# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
end
Here you override respond to eliminate recall.
4) Then inside config/initializers/devise.rb you add these lines:
config.warden do |manager|
manager.failure_app = CustomFailure
end
Basically, here you tell devise to look at the custom_failure.rb file you just created in case of a failure.
5) Finally, in your config/application.rb add these lines to your module:
config.autoload_paths << Rails.root.join('lib')
Here you tell Rails to automatically load your 'lib' folder and all the files that are in them (this step is not mandatory for all users it seems, but my rails app did not already automatically load the 'lib' files).
I hope this helped some of you.
Enjoy your code.
Best,
Ist
Rails Devise: How to access sign up page after signed in?
The way I handled the scenario of being able to create New Users if you are signed in was by generating a User controller and having new and create action methods that would save to the User model. (This was stripped from a current app I'm working on so hopefully I didn't miss anything)
user_controller.rb
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Successfully created User."
redirect_to root_path
else
render :action => 'new'
end
end
views/user/new.html.erb
<%= form_for @user, :url => user_index_path do |f| %>
<p><%= f.label :email %>
<%= f.text_field :email %></p>
<p><%= f.label :password %>
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Save" %></p>
<% end %>
config/routes.rb (Rails 3)
resources :user, :controller => "user"
Link to the New User page
<%= link_to 'New User', new_user_path %>
How to have two different signup pages in rails (using devise)
In your Devise view, just display different things based on the request url:
<% if request.fullpath.include?('signup_new') %>
<%= 'text 1' %>
<% else %>
<%= 'text 2' %>
<% end %>
Side note: you really should NOT have 2 different urls pointing to the same content, for Google it is called duplicate content.
Devise: Sign Up Page as Welcome/Landing Page then to User Profile
This my new and updated way using Rails 3.1.0
and Devise 1.5.0
:
routes.rb
root :to => "pages#redirect_to_sign_up"
devise_for :users do
get "welcome" => "devise/registrations#new", :as => :new_user_registration
get "account_settings" => "devise/registrations#edit"
get "sign_in" => "devise/sessions#new"
get "sign_out" => "devise/sessions#destroy"
get "new_password", :to => "devise/passwords#new"
end
match 'home', :to => "user_pages#home"
namespace :user do
root :to => "user_pages#home"
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def after_sign_in_path_for(resource)
stored_location_for(:user) || root_path
end
private
def after_sign_out_path_for(resource)
stored_location_for(:user) || root_path
end
end
pages_controller.rb
class PagesController < ApplicationController
def redirect_to_sign_up
if signed_in?.blank?
redirect_to new_user_registration_path
else
redirect_to home_path
end
end
end
user_pages_controller.rb
class UserPagesController < ApplicationController
before_filter :authenticate_user!
def home
end
def profile
end
end
Related Topics
How to Validate Ssl Certificate Chain in Ruby with Net/Http
Rails 4.2 - Sidekiq Not Sending Emails in Development
Bundle Install: Error: Failed to Build Gem Native Extension. Nio4R Gem
How to Install Rvm Without Root Access
Using Polymorphic Paths with Nested Associations
How to Dynamically Call Routes Helper in Rails
Difference Between Add_Dependency and Add_Runtime_Dependency
Ruby/Rails Using || to Determine Value, Using an Empty String Instead of a Nil Value
How to Do a Regex Search in Nokogiri for Text That Matches a Certain Beginning
Rails: How to Autocomplete Search for Name But Save Id
Rails 3.2.2 (Or 3.2.1) + Postgresql 9.1.3 + Ubuntu 11.10 Connection Error
Gem File with Git Remote Failing on Heroku Push
Group_By in Rails by 2 or More Attributes
How to Delete Special Characters
Error Message "Xcode Alone Is Not Sufficient on Sierra"