tbrooke
tbrooke

Reputation: 2197

Rails nested Resources not working in controller

I have this nested resource:

resources :services do
  resources :users  do
    put "assign" => "services#users#assign", as: :assign
  end 
end

My form contains this:

<%= button_to 'submit', service_user_assign_url(service.id, abstractor.id), method: :put %>

this generates the following url, which looks fine to me:

http://localhost:3000/services/1/users/2/assign

and the following is in my services controller:

def assign
  @service = Service.find(params[:service_id])
  @service.users << User.find(params[:user_id])
  redirect_to dashboards_path
end

However I get this error:

 The action 'users' could not be found for ServicesController

I'm not sure what this means - I have a has and belongs to many relationship between users and services and I am trying to associate an existing user to a service

Upvotes: 0

Views: 254

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42789

You need to tell rails if this action is a member action or a collection action, from the url you mentioned that you want to use, it's a member action:

resources :services do
  resources :users  do
    member do
      put :assign
    end
  end 
end

Upvotes: 2

Related Questions