Ruby on Rails - Remove Header and Sidebar AdminLTE - ruby-on-rails

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

Related

Rails routes for a static landing page if user not logged in

In my application I am using the Devise authentication gem which works really well. I also have built a static landing page which I currently have in the /public directory.
I can of course browser to localhost:3000/landing and see the page (as per the route below) but what I'm trying to achieve but cannot seem to figure out is how to setup my routes.rb file so that the landing.html file is the root, but when a user is logged in, their root becomes companies#index.
Here is my routes.rb file
Rails.application.routes.draw do
devise_for :users
get 'dashboard/index'
get '/landing', :to => redirect('/landing.html')
root 'companies#index'
resources :companies do
resources :shareholders
resources :captables do
post :subscribe_to_captable
resources :events do
post :lock_event
post :unlock_event
resources :transactions
end
end
end
end
Here is my application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
end
One way is to put the redirect inside your CompaniesController.rb index method do this.
CompanesController.rb (or whatever it is called)
def index
unless user_signed_in?
redirect_to landing_pages_path (or whatever the name of the route is)
end
end
Then change the route in the routes.rb file.
get '/landing', to: 'companies#landing' * change companies here to the name of the controller holding the landing page's method if it is not in the companies controller.

Ruby on Rails Separate admin folder, controllers and layouts

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

How do you customize the route after a user logs-in or signs-in in Devise?

Currently, a user gets redirected to a root index page after sign-in/log-in but I want to customize it so that the redirect goes to a different page.
My route file is:
Rails.application.routes.draw do
devise_for :admins, path: 'admins'
root 'home#index'
get '/' => "courses#index", as: :user_root
devise_for :users, path: 'users'
resources :courses, :lessons
end
I understand that by default, the root become where the redirect goes to so I used the code get '/' => "courses#index", as: :user_rootand the redirect worked as I wanted to. However, when a user logs-out, the redirect tries to go to get '/' => "courses#index", as: :user_rootonce again which I do no want. Instead, I want the redirect when a user logs-out to go to root 'home#index'.
So essentially my question is how can I customize my root so I can achieve different redirects depending on whether a user logs-in/signs-in and logs-out?
You can use this:
class ApplicationController < ActionController::Base
private
def after_sign_in_path_for(resource)
user_root_path
end
def after_sign_out_path_for(resource_or_scope)
root_path
end
end
It is on devise wiki:
signing in
signing out

How do you link an authenticated page with Devise in Ruby?

I just set up Devise and things are working properly. However, for some reason, my page is not working together in authentication.
Here is my routes.rb file.
devise_for :users #, path_names: { sign_out: 'sign_out' }
devise_scope :user do
authenticated :user do
root 'home#index'#, as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
get "/dashboard" => "home#index"
When I type in localhost:3000, it redirects me to the devise sign in page that exists on localhost:3000/users/sign_in.
When I type in localhost:3000/dashboard, it takes me to the sign in page as intended but when I enter my credentials, it keeps on bringing me back to the same sign in page over and over again.
Here is my application_controller.rb file.
protect_from_forgery with: :exception
before_filter :authenticate_user!
My intention is to have localhost:3000 redirect me to an authenticated version of localhost:3000/dashboard where I can see everything on that page. Not sure what's happening.
I suggest you to rewrite your code:
Remove before_filter :authenticate_user! as problem with unauthorized users already handled by routes.
Remove devise_scope :user do ... end block - there is a simpler solution (below).
Your routes.rb :
authenticated :user do
root to: redirect('/dashboard'), as: :authenticated_root
end
root to: redirect('/users/sign_in')
get "dashboard" => "home#index"

Set default route to devise login view for rails server

I am trying to configure my rails server so that http://localhost:3000/ redirects to http://localhost:3000/admin_users/sign_in . This is the Devise login view for my AdminUser model.
I have tried the following in my routes file without success:
devise_for :admin_users
root :to => "devise/sessions#new"
The following presents the login form but fails when I try to login due to too many redirects:
devise_for :admin_users do
root :to => "devise/sessions#new"
end
Any guidance would be appreciated!
it has nothing to do with routing
add this line to your applications controller
before_filter :authenticate_user!
your application will ask for login before allowing user to visit any page
to skip authentication on any action just put this, just put this line in that action's controller
skip_before_filter :authenticate_user!, :only => [:some_action]
you need to define the root route of your application so that after authentication a user can be redirected there eg:-
root to: 'users#index'

Resources