akmur
akmur

Reputation: 1635

Basic nested resources

I am nesting resources like so:

resources :users do
  resources :tags
end

And in my tags index page, I want to link to the single tag page, so I am doing like:

<%= link_to 'Show', user_tag_path(@user.id) %>

In my controller I'm passing the curretly logged in user id:

@user = current_user

Unfortunately, I'm getting the following error:

No route matches {:action=>"show", :controller=>"tags", :user_id=>1}

What am I doing wrong here?

Upvotes: 0

Views: 26

Answers (1)

deefour
deefour

Reputation: 35370

A Tag resource is dependent on a specific User resource. This means for a Tag's :show route user_tag, it looks like this

user_tag GET  /users/:user_id/tags/:id(.:format)  tags#show

You need to specify both a User and a Tag on this route, like

user_tag_path(@user, @some_tag_here)

However, you say

I want to link to the single tag page

which is poorly worded. I assume by this you mean you want to link to the :index route for Tag, specific to some User resourece. There is no "single tag page" defined like /tags.

The :index route looks like

  user_tags GET  /users/:user_id/tags(.:format)  tags#index

and is used like

user_tags_path(@user)

Upvotes: 1

Related Questions