How to create a route for two namespaces - ruby-on-rails

I'm trying to split my rails project in a front-end for regular users and a back-end for admins. Therefore i have created a namespace 'admin' so that i can easily control admin.After creating the admin namespace the I changed the routes from
Rails.application.routes.draw do
authenticated :user do
root to: 'dashboard#index', as: :authenticated_root
end
unauthenticated do
root to: "home#index"
end
match '(errors)/:status', to: 'errors#show', constraints: { status: /\d{3}/ }, via: :all
devise_for :users, skip: [:registrations]
as :user do
get 'my/profile/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
patch 'my/profile' => 'devise/registrations#update', as: 'user_registration'
end
resources :users
resources :events do
patch :archive, :unarchive
end
end
to this
Rails.application.routes.draw do
namespace :admin do
authenticated :user do
root to: 'dashboard#index', as: :authenticated_root
end
unauthenticated do
root to: "home#index"
end
match '(errors)/:status', to: 'errors#show', constraints: { status: /\d{3}/ }, via: :all
devise_for :users, skip: [:registrations]
as :user do
get 'my/profile/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
patch 'my/profile' => 'devise/registrations#update', as: 'user_registration'
end
resources :users
resources :events do
patch :archive, :unarchive
end
end
end
After these change I got this page
Rails::WelcomeController#index as HTML
Does anyone know how to do this?

If I understand what you ask, you want to put everything admin related into the admin namespace, but leave everything (for example, the root page) outside.
But in your routing example, you put everything inside the admin namespace, even the root page.
So generally, you want something like:
Rails.application.routes.draw do
namespace :admin do
# put admin stuff here
end
# put everything NOT in the admin interface outside your namespace
# you want a root route here. That's the page that'll be displayed by default
root to :your_root_stuff
# and if you have users who aren't admins, devise and authenticated routes too
# ... other stuff
end

Related

Rails Devise, No route matches '/users/sign_out' -> SignOut/Logout Routes problem

I have a app using devise to login/out, and view/create profiles. As of yet users may create and delete profiles although there is only supposed to be one profile per user. I have set up my routes just about without problems until comes the SignOut/LogOut from the whole app. Ultimately the error log is all I can decipher, and seems that routes.rb needs some modification for this to work, but I am stumped. Here are the errors and routes.rb:
/log/production.log:
Started DELETE "/users/sign_out" for 127.0.0.1 at 2020-01-26 01:56:53 -0500
ActionController::RoutingError (No route matches [DELETE] "/users/sign_out"):
routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => {:sessions => "users/sessions" }
resources :profiles, only: [:new, :create, :edit, :update, :destroy]
devise_scope :user do
authenticated :user do
root to: 'profiles#index', as: :authenticated_root
get '/profiles/new' => 'profiles#new'
match '/profiles' => 'profiles#create', via: [:get, :post]
get '/profiles/:id' => 'profiles#show'
get '/profiles/:id/edit' => 'profiles#edit'
match '/profiles/:id' => 'profiles#update', via: [:get, :post]
delete '/profiles' => 'profiles#destroy', via: [:get, :post]
end
unauthenticated :user do
root to: 'devise/sessions#new', as: :unauthenticated_root
match '/users/sign_in' => 'devise/sessions#create', via: [:get, :post]
delete '/users/sign_out' => 'devise/sessions#destroy'
end
end
end
I read that using resources :users may affect devise sessions controller, in that I would need a UsersController, however haven't included resources :users in my routes, and/or for a similar error.
You placed the sign-out route in the unauthenticated block in your routes.rb
unauthenticated :user do
# ..
delete '/users/sign_out' => 'devise/sessions#destroy'
end
What doesn't makes sense, only authenticated users can sign out. Just move that method into the authenticated :user block above:
authenticated :user do
# ..
delete '/users/sign_out' => 'devise/sessions#destroy'
end

Devise - Logging out of one Namespace, logs out of all Namespaces

My app has "users" for the main app and "admin_users" and the /admin Namespace.
When I'm logged into the /admin Namespace as an admin_user, and click "log out" - Devise logs me out of both Namespaces. How can I ensure that Devise only logs me out of that Namespace?
Routes.rb
devise_for :users, skip: :all
as :user do
get 'signin', to: 'devise/sessions#new', as: :new_user_session
post 'signin', to: 'devise/sessions#create', as: :user_session
delete 'signout', to: 'devise/sessions#destroy', as: :destroy_user_session
end
devise_for :admins, skip: :all
as :admin do
get 'admin/signin', to: 'admin/devise/sessions#new', as: :new_admin_session
post 'admin/signin', to: 'admin/devise/sessions#create', as: :admin_session
delete 'admin/signout', to: 'admin/devise/sessions#destroy', as: :destroy_admin_session
end
authenticate :admin do
namespace :admin do
...
end
end
set config.sign_out_all_scopes = false in config/initializers/devise.rb.
# Set this configuration to false if you want /users/sign_out to sign out
# only the current scope. By default, Devise signs out all scopes.
config.sign_out_all_scopes = false

Cannot logout of rails application devise gives error Could not find devise mapping for path "/tuners/sign_out

I have this issue when loging out of devise:
Could not find devise mapping for path "/tuners/sign_out". This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/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]
My routes file
Rails.application.routes.draw do
devise_for :tuners
get '/tuners/sign_out' => 'devise/sessions#destroy'
resources :hardy_pianos
devise_for :admins
resources :images
resources :entries
resources :models
get '/clients/mail_room' => 'clients#mail_room'
get '/clients/mail' => 'clients#mail'
#list of clients to reschedule
get '/clients/book_again' => 'clients#book_again'
# current upcoming jobs
get '/clients/current' => 'clients#current'
# reports
get '/clients/reports' => 'clients#reports'
resources :clients
seems_rateable
post '/rate' => 'rater#create', :as => 'rate'
resources :technicians
resources :members
resources :admins
resources :tuners
get 'tuners/tuners_pianos'
get 'pianos/tuners_pianos'
resources :tuners
resources :journals
resources :expenses
resources :interests
resources :purchases
resources :contacteds
resources :pianos
get '/pianos/work_needed' => 'pianos#work_needed'
get '/invoices/work_needed' => 'invoices#work_needed'
resources :invoices
resources :pages
#devise_for :installs
resources :prosperities
get '/finances/taxes' => 'finances#taxes'
resources :posts
get '/tunings/current' => 'tunings#current' # or match for older Rails versio
get '/tunings/bookagain' => 'tunings#bookagain' # or match for older Rails version
get '/tunings/bookagain12' => 'tunings#bookagain12'
get '/tunings/reports' => 'tunings#reports'
get '/tunings/information' => 'tunings#information'
resources :tunings
root 'pages#index'
get '/' => 'tunings#current'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Can anyone help me understand what the issue is here with this? I'm kind of stuck with this and have googled for a while but left it on the back burner. Thanks in advance.
Replace the line:
get '/tuners/sign_out' => 'devise/sessions#destroy'
with
delete 'sign_out', to: 'devise/sessions#destroy'
or can you try this this. watch the order of lines.:
Rails.application.routes.draw do
devise_scope :tuners do
get "sign_out", to: "devise/sessions#destroy"
end
devise_for :tuners

show errors in custom page rather than in default page while login/signup with devise

I have put login and signup in one page and every thing works fine except when I encounter errors. Then the page redirects to their default pages and show errors there. In my case the login redirects me to the default domain.com/users/sign_in , but signup redirects me to domain.com/users.
routes.rb
Rails.application.routes.draw do
root 'visitor#index'
namespace :admin do
# get "/stats" => "stats#stats"
devise_scope :admin_user do
get '/stats/:scope' => 'stats#stats', as: :admin_stats
end
end
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
namespace :client do
get 'dashboard' => 'dashboard#index', as: 'dashboard'
# resources :verification, only: [:create, :index, :destroy]
get 'verification' => 'verification#index', as: 'verification'
match 'verification' => 'verification#upload', as: 'verification_upload', via: [:post, :patch]
end
devise_for :users, class_name: 'FormUser', controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }
# devise_scope :user do
# root to: 'devise/registrations#new'
# end
end
you can use a CustomFailure class to control where the redirect goes if Devise fails to authenticate.
It's explained at this wiki page...
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

How to change the home page of my Rails app to show Registration page instead?

I would like to make the homepage for an app (ie landing page) display a registration page. Unless the user is logged in - in which case they just find the "statuses" page.
Here are what I believe are the two relevant excerpts:
as :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
end
[...]
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
Basically, I'm trying to get these two pages to swap their routes and route names. Unless, as mentioned, if someone is already signed in, then the landing page is the statuses page.
I only know a bit about this sort of thing, such as "get," and the URL aspects. If anyone could provide guidance, I'd be much obliged.
Here's the routes.rb file:
Treebook::Application.routes.draw do
resources :activities, only: [:index]
as :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
end
devise_for :users, skip: [:sessions]
as :user do
get "/login" => 'devise/sessions#new', as: :new_user_session
post "/login" => 'devise/sessions#create', as: :user_session
delete "/logout" => 'devise/sessions#destroy', as: :destroy_user_session
end
resources :user_friendships do
member do
put :accept
put :block
end
end
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
scope ":profile_name" do
resources :albums do
resources :pictures
end
end
get '/:id', to: 'profiles#show', as: 'profile'
end
Why not just send the user to the statuses page, and redirect all users who are not signed in to the sign in?
class StatusesController
before_action :authorize!, only: :index
def index
#...
end
def authorize! # I believe this method is provided by devise
unless signed_in?
redirect_to new_user_session_path
end
end
end
# config/routes.rb
root 'statuses#index'

Resources