ruby on rails - Linking HABTM Attribute -


i'm trying create link new page displays posts belong category. they're both connected via habtm relationship can see below.

category.rb

class category < activerecord::base     has_and_belongs_to_many :posts end 

post.rb

class post < activerecord::base     has_and_belongs_to_many :categories     belongs_to :user end 

index.html.erb

<%= post.categories.collect(&:name).join(",") %> 

how go creating link posts category attribute page displays posts category?

any brilliant!

thanks

this should work:

<% post.categories.each |category| %>   <% category.posts.each |post| %>     <%= link_to post.title, post_url %>   <% end %> <% end %> 

change post.title , post_url appropriately.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -