Trenton Tyler
Trenton Tyler

Reputation: 1712

Couldn't find Group with 'id'= Rails

I have this method in my groups controller that is looking for the id of the group that is passed in the params.

def set_group
  @group = Group.find(params[:id])
end

I have this route, and a couple others, that are looking for the group_id rather than the id. Since it is not finding the group_id it is throwing this error.

/accounts/:account_id/groups/:group_id/settings

Couldn't find Group with 'id'=

How can I tell this variable to find the params of the id and or the group_id? I have listed all my routes for this particular instance below and half require the id and the other require the group_id.

/accounts/:account_id/groups/:group_id/add_map(.:format)  
/accounts/:account_id/groups/:group_id/add_map(.:format)  
/accounts/:account_id/groups/:group_id/maps(.:format)   
/accounts/:account_id/groups/:group_id/add(.:format)   
/accounts/:account_id/groups/:group_id/add(.:format) 
/accounts/:account_id/groups/:group_id/remove/:id(.:format) 
/accounts/:account_id/groups/:group_id/maps/:map_id/remove(.:format) 
/accounts/:account_id/groups/:group_id/settings(.:format)          
/accounts/:account_id/groups(.:format)                               
/accounts/:account_id/groups(.:format)                               
/accounts/:account_id/groups/new(.:format)                           
/accounts/:account_id/groups/:id(.:format)                           
/accounts/:account_id/groups/:id(.:format)                           
/accounts/:account_id/groups/:id(.:format)                           
/accounts/:account_id/groups/:id(.:format)

Would I just need a separate variable with the group_id?

Upvotes: 0

Views: 434

Answers (1)

Cody Caughlan
Cody Caughlan

Reputation: 32758

You could look for one or the other in the params and use the first one you find, like:

def set_group
  id = params[:group_id] || params[:id]
  @group = Group.find(id)
end

Upvotes: 1

Related Questions