Question

I have three models. User, Product, and Season.

I used a standard "has many through" approach:

user has_many :seasons
user has_many :products, :through => :seasons

product has_many :seasons
product has_many :users, :through => :seasons

seasons belong_to :product
seasons belong_to :user

On my "show" view for my users, I display the user's products. I do this as follows:

    <% @user.seasons.each do |c| %>
      <%=h c.product.name %>
    <% end %>  

This all works great.

Here's my question. How do I create a hyperlink to the show view of whatever product is generated by this code <%=h c.product.name %>? I followed the API and tried using a block, but none of my attempts worked properly.

Was it helpful?

Solution

  <% @user.seasons.each do |c| %>
    <%= link_to c.product.name, c.product %>
  <% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top