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
Related
I accessed localhost/3000/login_users/sign_up and registered a user.
After that, I accessed localhost/3000/login_users/sign_in. However, it will always be redirected to localhost:3000.
I will post routes.rb. I will post the serverlog.
Why is that?
Rails.application.routes.draw do
devise_for :login_users
# For details on the DSL available within this file,
# see http://guides.rubyonrails.org/routing.html
end
you can redirect user to a specific path after sign in by defining a method in application controller.
def after_sign_in_path_for(resource)
some_path
end
by default devise redirect to root path. you can set a root path is routes.rb also as following:
root :to => 'testers#index'
How to hide header and sidebar AdminLTE, just for signup page? For login, it generates automatically when I run this command rails g devise:views users. How about signup page?
Here is my routes.rb :
Rails.application.routes.draw do
devise_for :users, controller: {
sessions: "sessions/registrations"
}
get 'home/index'
root :to => 'home#index'
end
and my application_controller.rb :
class ApplicationController < ActionController::Base
before_action :authenticate_user!
layout 'admin_lte_2'
end
I don't know how you exactly use devise with admin lte but I can see 2 possible solutions
Option 1
You could define a different route for logged in users:
devise_for :users
devise_scope :user do
authenticated :user do
root 'home#index', as: :authenticated_root
end
get 'user', to: 'devise/sessions#new'
end
Unauthenticated users will be redirected to devise/session#new view (or any other of your choice).
If you would like to have another content shown for those users other than registration page, add unauthenticated root:
unauthenticated do
root 'foo#bar', as: :unauthenticated_root
end
Option 2
Create your custom registration page without header and sidebar. I think it's easier to edit view generated by devise (I don't know if it's good practice tho) than create a custom layout.
You will have probably a view:
app/views/devise/registrations/new.html.erb
just edit it so it will suit your needs.
For creating a custom layout for devise look here:
Wiki: Create custom layouts
Wiki: Customize routes
I hope it helps
I have a rails 5.2 application, using devise 4.4.3, which has two devise models: User and Parent.
The paths are defined the config/routes.rb as:
devise_for :users, path: 'users'
devise_for :parents, path: 'parents'
Currently when I visit an action that required authentication in an unauthenticated session I'm getting redirected to a /parents/sign_in, but I want to specify /users/sign_in as the default unauthenticated path.
How can I achieve this?
Add an unauthenticated call and define the default root route:
unauthenticated do
root to: "users#sign_in"
end
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
I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
namespace namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin. That's what admin/admin#index means. :)
According to rubyonrails.org guide on routing you should be able to do something like this.
it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
on rails 4 in routes two roots not permited now
use
get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index and other methods to be in straightforward AdminController controller in usual directory, views to go in views/admin etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end