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?

Would be great if anyone could point me in the right direction. Much
appreciated!!

Thanks!
Jason

--
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@googlegroups.com.
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.

Search Discussions

  • Colin Law at Mar 29, 2012 at 7:12 am

    On 27 March 2012 22:10, @jikche 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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 28, '12 at 10:57p
activeMar 29, '12 at 7:12a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Colin Law: 1 post @jikche: 1 post

People

Translate

site design / logo © 2022 Grokbase