I’m working on a way to construct a guest list of an event called a Party. The user_id of each invited guest is stored in a hidden field, separated by spaces.
<%= hidden_field_tag 'guestlist', nil, :class => 'guest_list' %>
When a user invites a new guest, the guest’s id is added to the field and stored.
I’m trying to figure out a way so that when a User edits an existing party, the id’s of the already invited guests show up in the hidden guest_list field.
Is there a way to do this with embedded Ruby? Something along the lines of:
<% @party.guests.each do |guest| %>
Do something here
<% end %>
Try this (in 2 steps for readability):
The first line will call
.idon each guest and then collect them in an array (like[1, 4, 12]) and then the.joinwill put them in to a string where it will use the","to separate them.EDIT The
.map(&:id)is the same as saying:But it’s condensed in to that
&:id. The.idis obviously the method name we want to call and whose results we want to collect, and the & basically converts that in to a block. You can read this for some more information, or perhaps better, this SO post.