Is there a method to change URL using namespaces? - ruby-on-rails

I'm trying to change the URL name.
This is my routes.rb:
namespace :user_management do
resources :user do
collection do
get 'main'
end
end
end
match ':controller(/:action(/:id(.:format)))', :via => [:get, :post]
Rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / user_management/login#login
user_main_user_management_user_index GET /user_management/user/main(.:format) user_management/user#main
user_management_user_index GET /user_management/user(.:format) user_management/user#index
POST /user_management/user(.:format) user_management/user#create
new_user_management_user GET /user_management/user/new(.:format) user_management/user#new
edit_user_management_user GET /user_management/user/:id/edit(.:format) user_management/user#edit
user_management_user GET /user_management/user/:id(.:format) user_management/user#show
PATCH /user_management/user/:id(.:format) user_management/user#update
PUT /user_management/user/:id(.:format) user_management/user#update
DELETE /user_management/user/:id(.:format) user_management/user#destroy
GET|POST /:controller(/:action(/:id(.:format))) :controller#:action
My URL is:
localhost/user_management/user/main
And I want:
localhost/user_main
I tried this but it is not working:
namespace :user_management do
resources :user do
collection do
get 'main', as: :user_main
end
end
end
I tried this but is not working either:
namespace :user_management do
resources :user do
collection do
get '/user_main', as: "user_management/user#main
end
end
end

try
match "user_admin", :to => "user_management/user#main"

you can do like in example:
get 'exit', to: 'sessions#destroy', as: :logout # url => /logout
FROM RAILS ROUTING

Related

need help on rails nesting routes

I want to get a route like
GET /example/notifications/status, to: example/notification#status
POST /example/notifications/disable to: example/notification#disable
Here is what I did
resources :example, only => %i[index] do
collection do
resources :notifications, :only => [] do
collection do
get :status
post :disable
end
end
end
end
it get the right path but it point to notification#status not example/notification#status
is there any way I can get both right path and controller expect code like
get "notifications/status", :to => "example/notifications#status"
You can use namespace for this purpose like below,
in you config/routes.rb file
namespace :example do
collection do
get :status
post :disable
end
end
It will hit the example/notification#status action.
for more information see here
Don't use resources since you don't want (aparently) any param on your routes. Go for namespace instead:
namespace :example do
namespace :notifications do
get 'status'
post 'disable'
end
end
Gives you:
$ rails routes -g example
Prefix Verb URI Pattern Controller#Action
example_notifications_status GET /example/notifications/status(.:format) example/notifications#status
example_notifications_disable POST /example/notifications/disable(.:format) example/notifications#disable

Rails 4 - How to match routes in namespace

Hi i have admin panel controller and have many controller in admin panel.
I want to match routes usually without namespace i've used
match ':controller(/:action(/:id))', :via => [:get, :post]
I want this in namespace controller my current
router.rb
namespace :admin do
get '', to: 'dashboard#index', as: '/'
get 'dashboard/index'
##AUTHENTICATION
get 'login/index'
get 'login/logout'
post 'login/attempt_login'
get 'login/attempt_login'
##PAGES
get 'pages/index'
get 'pages/add_new'
get 'pages/edit'
post 'pages/create'
post 'pages/update'
post 'pages/task'
get 'pages/task'
##USERS
get 'users/index'
get 'users/edit'
get 'users/delete'
get 'users/destroy'
get 'users/update'
get 'users/add_new'
post 'users/create'
post 'users/update'
post 'users/task'
#USER GROUPS
get 'user_group/index'
get 'user_group/add_new'
get 'user_group/edit'
post 'user_group/create'
post 'user_group/update'
post 'user_group/task'
#USER GROUPS
get 'access_sections/index'
get 'access_sections/add_new'
post 'access_sections/create'
post 'access_sections/update'
post 'access_sections/task'
end
Any solution please?
You simply wrap the routes you're declaring in a namespace like so:
namespace :login do
get 'index'
get 'logout'
end
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
For example we have orders which can be cancelled:
Following description in routes.rb
resources :orders do
post :cancel, to: 'orders/cancellations#cancel'
end
Will send request to app/controllers/orders/cancellations_contoller.rb
module Orders
class CancellationsController
def cancel
#order = Order.find(params[:id]).cancel
end
end
end
It's useful to refactor controller resourses with many service methods.
Wish it helps

Set default controller but not to affect other controller and paths

I want to replace root to welcome controller,
But if I use the url http://localhost:3000/welcome/portfolio The action 'welcome' could not be found for WelcomeController
How to not affect original controller with the routes rule match '/:action(/:id)', :controller => "welcome",via: [:get, :post]
rake up routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
GET|POST /:action(/:id)(.:format) welcome#:action
portfolio_welcome_index GET /welcome/portfolio(.:format) welcome#portfolio
welcome_index GET /welcome(.:format) welcome#index
POST /welcome(.:format) welcome#create
new_welcome GET /welcome/new(.:format) welcome#new
edit_welcome GET /welcome/:id/edit(.:format) welcome#edit
welcome GET /welcome/:id(.:format) welcome#show
PATCH /welcome/:id(.:format) welcome#update
PUT /welcome/:id(.:format) welcome#update
DELETE /welcome/:id(.:format) welcome#destroy
route.rb
root :to => "welcome#index"
match '/:action(/:id)', :controller => "welcome",via: [:get, :post]
resources :welcome do
collection do
get 'portfolio'
end
end
it happens because /:action(/:id) handel each path with format /something/some_id and even /something so you could put it to the end in routes file:
resources :welcome do
collection do
get 'portfolio'
end
end
match '/:action(/:id)', :controller => "welcome",via: [:get, :post]
root :to => "welcome#index"
in this case request to /welcome/portfolio will be handled with resources definition before going to your /:action(/:id) definition.

Reduce complexity of routes

I am attempting to setup my routes.rb so that /sessions/ is not required in the url for logging in and out of the site. Below are my samples to show what I am trying to achieve. Whilst the "second attempt" does in fact do what I want, I'd like to know if there is a more efficient way of doing this. I am very new to rails and I am sure that the routes.rb has some option that can do what I am doing in three large lines.
First attempt
routes.rb
namespace :account do
resources :users
resources :sessions
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_sessions GET /account/sessions(.:format) account/sessions#index
POST /account/sessions(.:format) account/sessions#create
new_account_session GET /account/sessions/new(.:format) account/sessions#new
edit_account_session GET /account/sessions/:id/edit(.:format) account/sessions#edit
account_session GET /account/sessions/:id(.:format) account/sessions#show
PATCH /account/sessions/:id(.:format) account/sessions#update
PUT /account/sessions/:id(.:format) account/sessions#update
DELETE /account/sessions/:id(.:format) account/sessions#destroy
Second attempt
routes.rb
namespace :account do
resources :users
match '/login', :controller => 'sessions', :action => 'new', :via => [:get]
match '/login', :controller => 'sessions', :action => 'create', :via => [:post]
match '/logout', :controller => 'sessions', :action => 'destroy', :via => [:delete]
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_login GET /account/login(.:format) account/sessions#new
POST /account/login(.:format) account/sessions#create
account_logout DELETE /account/logout(.:format) account/sessions#destroy
Can this be done without having to manually specific the match locations? All I want to do is remove /sessions/ as a requirement.
namespace :account do
resources :users #-> account/users
resources :sessions, path: "", path_names: { new: "login", create: "login", destroy: "logout" } #-> accounts/login, accounts/logout
end
I hope you realise you have /login twice in your second example. This simplifies it a bit but you will always have to match each route you want to specify outside any defaults.
namespace :account do
match '/login', to: 'sessions#new', via: [:get]
match '/logout', to: 'sessions#destroy', via: [:delete]
end
In rails3 we should use with_options in following way:
scope '/account' do
match '/login' => "sessions#new", :as => :login
post '/:login' => 'sessions#create', :as => :signup_create
delete '/:logout' => 'sessions#destroy', :as => :logout
end

How to make a route both for get and post?

I have this:
resources :users do
collection do
get 'blah'
end
end
I want to make this action (blah) for both post and get now, possible?
Looks like verb constraints is what you want.
match 'blah', to: 'users#blah', via: [:get, :post]
or
resources :users do
collection do
match 'blah', via: [:get, :post]
end
end
You could just enter the same name for the post route like this:
resources :users do
collection do
get 'blah'
post 'blah'
end
end
Both routes will have the same controller, action and url_helpers

Resources