cilphex
cilphex

Reputation: 6096

Why does this route setup result in these path variables?

In my rails 4 application, I have a piece in routes.rb that looks like this:

namespace :settings do
  resources :profile, only: [:index] do
    put :update_user, on: :collection
  end
end

The controller is located in app/controllers/settings/profile_controller.rb and looks like this:

class Settings::ProfileController < ApplicationController

  def index
  end

  def update_user
  end

end

This results in these paths from rake routes:

PUT update_user_settings_profile_index -> /settings/profile/update_user(.:format)

GET settings_profile_index -> /settings/profile(.:format)

What I don't understand is why these paths have _index in them. I would like to get rid of it. Can I? If so, how?

Upvotes: 0

Views: 27

Answers (2)

Elvn
Elvn

Reputation: 3057

For me, with routes it is always easier to work backwards, asking what are the paths I want? I'm not sure what paths you want, but I'll give you some ideas here, I hope.

I'm going to assume you want one index path, since you explicitly include the the only: [:index] statement, but you do not want not a indexed path on your resources.

Try moving your only: [:index] statement into the outer do loop for settings and add an only: [:update] to your profile (or whichever action you're looking for)

  namespace :settings, only: [:index] do
      resources :profile, only: [:update] do
        put :update_user, on: :collection
      end
    end

Gets you here:

update_user_settings_profile_index PUT    /settings/profile/update_user(.:format)                settings/profile#update_user
                  settings_profile PATCH  /settings/profile/:id(.:format)                        settings/profile#update
                                   PUT    /settings/profile/:id(.:format)                        settings/profile#update

Upvotes: 0

Almaron
Almaron

Reputation: 4147

I think that's because you're using a singular name profile for your resources definition, which should be plural by convention. You can try using resources :profiles instead.

Upvotes: 3

Related Questions