On 27 March 2012 22:10, @jikche
<jik-xadSX2Mwn7rQT0dZR+AlfA@public.gmane.org>
wrote:> Hi everyone, I''m new to ruby/rails and trying to build a simple
> Projects / Tags app where Projects and Tags are associated as
> has_and_belongs_to_many to each other. It''s basically a simple
list of
> projects that have tags associated, and those tags in turn can be re-
> used by multiple projects.
>
> I''ve been loading tags into projects like this:
>
> my_project = Project.create(:name => "My Project")
> my_tag = Tag.create(:content => "My Tag")
> my_project.tags << my_tag
>
> All appears well until I try to load the info into my View. I have a
> list of projects, each with a small table below that lists the
> associated tags:
>
> <table>
> <% @projects.each do |project| %>
> <table>
> <tr>
> <td width="300"><%= project.name %></td>
> <td width="100"><%= link_to ''Edit
project'',
> edit_project_path(project) %></td>
> <td width="100"><%= link_to ''Nuke
project'', project, :confirm
> => ''Are you sure?'', :method => :delete
%></td>
> </tr>
> </table>
> <table>
> <tr>
> <td><%= project.tags %></td>
> </tr>
> </table>
> <% end %>
> </table>
>
> The View output I get is:
>
> [1] My Project Edit Project Nuke Project
> [2] [#<Tag id: 1, content: "My Tag", created_at:
"2012-03-27
> 19:27:26", updated_at: "2012-03-27 19:27:26">, #<Tag
id: 2, content:
> "My Other Tag", created_at: "2012-03-27 19:41:04",
updated_at:
> "2012-03-27 19:41:04">]
>
> In line [2] How do I go about only displaying the values of :content
> and not the entire hash? Also - is this how I should associate tags
> with projects?
project.tags is the complete set of tags for that project. To show
the individual tag details use an each loop on project.tags in the
same way as you have for the projects themselves, so something like
<% project.tags.each do |tag| %>
and display tag.whatever.
Colin
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.