Im using rails 4.1.1 and ruby 2.1.1 and am having an issue with devise, namely my routes..I have used this many times before
devise_for :users
get 'pages/index'
# Route to Devise Login Page
devise_scope :user do
root to: "devise/sessions#new"
end
# Directing the user after login
authenticated :user do
root :to => 'pages#index'
end
But i get the error
`add_route': Invalid route name, already in use: 'root' (ArgumentError)
when trying to start the server.. I can see that root is being used twice, but like i said i have been able to do this in the past.. Is there a way around this
Thanks
Found this helpful comment here on stackoverflow
For Rails 4.0 you have to make sure you have unique names for the path
helpers, like root to: "dashboard#show", as: :authenticated_root.
Otherwise the authenticated root and the normal root route end up
having the same name for their path helpers, which Rails 4.0 no longer
allows
so I changed my authenticated root to helper like so
# Directing the user after login
authenticated :user do
root :to => 'pages#index', as: :authenticated_root
end
Related
Just updated to rails 6 and having trouble with a conditional root route
devise_scope :user do
authenticated do
root to: 'users#show'
end
unauthenticated do
root to: 'visitors#index'
end
end
I've attempted to check for the logged-in user in visitors#index and do a redirect to users#show but now I have this ugly URL '/users/:id' instead of being able to visit users#show with a clean root URL.
In my case I was able to solve this by adding an :as argument:
devise_scope :user do
authenticated do
root to: 'users#show'
end
unauthenticated do
root to: 'visitors#index', as: :visitors_url
end
end
I think the reasoning behind the change is to make it so that Rails knows where to route root_url.
Note: I found the answer on reddit where it was suggested using namespace would be cleaner.
namespace :visitors, path: nil do
root to: 'visitors#index'
end
I don't know if there is a way to make that work with devise though.
I am new to Ruby on Rails. I want to have following structure for admin section.
app/controller/admin/admin_controller.rb and all other admin section controller under app/controller/admin/ folder
app/views/layout/admin/admin.html.erb to keep separate html layout for admin section
At the same time i want to use Devise Gem for admin and front end user authentication.
I executed rails g devise:views admin, rails generate devise Admin and rails g controller admin/home index command that created views, model and controller for admin user. Now what routes and other setting i need to add so that ruby could understand that if i type http://localhost:3000/admin/ then i should be redirected to http://localhost:3000/admins/sign_in/ page and after entering correct admin credentials i should redirected to index method of controllers/admin/home_controller.rb
is it also possible to keep singular convention of Devise admin views like admin/sign_in instead of admins/sign_in ?
I have searched a lot but could not get relevant help. Please provide steps to achieve above.
Thanks in advance.
This is how route file looks like
Rails.application.routes.draw do
namespace :admin do
get 'home/index'
end
devise_for :admins
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "home#index"
end
When i type http://localhost:3000/admin/ then i get below error
Your problem is that you do not have root route defined for /admin.
I have the same URL convention routes in one of the apps and routes.rb looks like this:
Rails.application.routes.draw do
# Admin part
devise_for :admins, path: '/admin'
scope module: :admin, path: '/admin', as: 'admin' do
root to: 'home#index'
end
# Redirect app root to client part
root to: redirect(path: '/panel', status: 301)
# Client part
devise_for :clients, path: '/panel'
scope module: :panel, path: '/panel', as: 'panel' do
...
end
end
I am new to Ruby on Rails,I am using Devise gem for authentication on table account_user.
When I do rake routes I get
new_account_user_session GET /account_users/sign_in(.:format)account_user/sessions#new
So my login page is xyz.com/account_users/sign_in.
I want to change the sign-in page to just xyz.com
I don't have any routes for the same in my routes.rb file, I thought devise is automatically generating routes for this.
Is there a way I can add alias/override for this devise generated routes, or redirect user to xyz.com instead of xyz.com/account_users/sign_in
set root to devise sign_in, so in your route file there should be
devise_for :account_users
devise_scope :account_user do
root to: 'devise/sessions#new'
end
this will set your root path to sign_in
or if you want to rename the route to 'login'
devise_for :account_users
devise_scope :account_user do
get 'login', to: 'devise/sessions#new'
end
more here https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
In a Rails 3.2 app I have two user models set up using Devise: Admin and User.
I have an Admin-specific root page defined in my routes file.
namespace :admin do
root :to => "pages#welcome"
end
I have also seen this written as
authenticated :user do
root :to => 'pages#welcome'
end
I have not been able to find a clear description of the difference between using namespace and authenticated, and what implications this may have for security.
I'd be very grateful if someone could enlighten me, or point me towards a clear description.
Thanks!
Namespace routes will always exist -- meaning you can always call
/admin and it will route to pages#welcome
Authenticated routes will only exist if the :user is logged in.
I use it to create a root_path that's a dashboard for a logged in user, but the marketing pages#welcome for non logged in:
authenticated :user do
root :to => 'profile#show'
end
root :to => 'pages#welcome'
Here is a link to devise doc for the method authenticated, which should answer your question : http://rdoc.info/github/plataformatec/devise/ActionDispatch/Routing/Mapper#authenticated-instance_method
Going demented with an issue here :-(
My requirement is as follows,
If a user visits myapp.com the root of my app is defaulted to promotional pages & sign-up form. This is achieved by checking for the presence of a subdomain.
If the user is not logged in and tries to visit their account at test.myapp.com they will be directed to test.myapp.com/users/sign_in -aka- devise/sessions#new
If the user is logged in (devise) and visits test.myapp.com the root of the application will be the application dashboard.
Here is what I am trying to use in my routes.rb
constraints(Subdomain) do
authenticated do
root :to => 'dashboard#index'
end
root :to => 'devise/sessions#new'
end
root :to => 'promo_pages#index'
Currently I have the following, you will note that the devise bit is not included.
constraints(Subdomain) do
authenticated do
root :to => 'dashboard#index'
end
end
root :to => 'promo_pages#index'
My problem with the latter is that when a user who is not logged in first visits test.myapp.com they are redirected to test.myapp.com/users/sign_in and an error message is displayed saying "You need to sign in or sign up before continuing." This is because I am enforcing a logon requirement for the dashboard pages.
However I don't want the user to get an error message the first time they visit the page, as it is ugly and makes it look like they have done something wrong when they have not.
My expectation is that if the user is not logged in then they will be directed straight to the logon page and not get an error notification. But when I use my amended version the following happens,
I can visit myapp.com just fine and it is routed to the promo pages
but if I try to visit test.myapp.com I get the following message in the browser
**Unknown action**
Could not find devise mapping for path "/". Maybe you forgot to wrap your route inside the scope block? For example: devise_scope :user do match "/some/route" => "some_devise_controller" end
Please advise what I am doing wrong (if anything) as I am going crackers trying to understand what to do.
Ps: I have found similar errors in stackoverflow and various googling but the solutions just don't seem to work for me. I expect that the solution to the problem lies in the error message that I have included above, but I can't figure out how to apply it.
Finally here is the log entry version of the error above, it is in an easier to read form.
Started GET "/" for 127.0.0.1 at 2012-01-15 21:44:42 +0000
Processing by Devise::SessionsController#new as HTML
[Devise] Could not find devise mapping for path "/".
Maybe you forgot to wrap your route inside the scope block? For example:
devise_scope :user do
match "/some/route" => "some_devise_controller"
end
Completed 404 Not Found in 1ms
AbstractController::ActionNotFound (Could not find devise mapping for path "/".
Maybe you forgot to wrap your route inside the scope block? For example:
devise_scope :user do
match "/some/route" => "some_devise_controller"
end
):
All help is appreciated, and additional details can be provided.
Thanks for reading
Update
I have just noticed that the 'authenticated' check does not appear to be working.
If it was working correctly then when using the second batch of working config, the logged in user visiting test.myapp.com would always be directed to the promo_pages, whereas at the moment he is able to access the dashboard..
I found the devise 'authenticated' method here
https://github.com/plataformatec/devise/pull/1147
Without the constraint, this will cause a Could not find devise mapping for path "/" error. This simple addition fixed it.
devise_scope :user do
authenticated :user do
root :to => 'dashboard#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
root :to => 'dashboard#index'
end
I have got it sorted,
Ok, firstly the reason I was getting the devise error was that I needed to place the devise root statement inside the "devise_for :users" block
Secondly,
The authenticated check was not working because I failed to include a scope as I was under the mistaken impression it was not necessary.
Here is the finalised code, note that in rails routing the priority is based on order of creation, first created is highest priority. Thus in this case the promo_pages controller is only considered root if nothing else was previously specified.
constraints(Subdomain) do
authenticated :user do
root :to => 'dashboard#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
end
root :to => 'promo_pages#index'