Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the prefix generated in routes.rb for the params in the path

Currently in my routes I have:

  # USER RESOURCES
  resources :users do
    resources :repositories
    patch 'change_password'
    get 'account_setting'
  end

Which generates this path for the account_setting action:

user_account_setting GET    /users/:user_id/account_setting(.:format)       users#account_setting

What I want is to have:

user_account_setting GET    /users/:id/account_setting(.:format)       users#account_setting

The two are essentially the same thing, but the first has a user_ prefix for the id which rails adds because it is in users resource block.

SIDE NOTE

I know I can simply remove the account_setting action from the users resource block and write:

get 'users/:id/account_setting', to: 'users#account_setting'

But I don't want to.

like image 326
Tucker Avatar asked Dec 25 '22 18:12

Tucker


1 Answers

You can do it as follows:

 resources :users do
      member do
        get 'account_setting'
      end
    end

To add a member route, add a member block into the resource block.

For documentation, you can check http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html

like image 77
Vrushali Pawar Avatar answered Dec 29 '22 12:12

Vrushali Pawar