I am having trouble finding any information on how to iterate through an array and create objects.
My form creates a selectable list of users that when checked, pass the user_ids as an array object.
invitations\new.html.rb
<%= bootstrap_form_for Invitation.new do |f| %>
<br>
<ul>
<%= f.hidden_field :attended_event_id, :value => @event_selected.id %>
<li>
<%= check_box_tag 'attendee_id[]', user.id %>
<%= h user.name %>
</li>
<% end %>
</ul>
<br>
<%= f.submit "Invite Selected Users" %>
<% end %>
I would like to then create new Invitations objects by combining the attended_event_id with all of the objects in the attendee_id array.
After a bit of trouble I got the basics of my controller working but only by passing in the user_id as a text entry. Below is my Invitations controller. Not really sure where to start on this one as I haven't been able to find a good example.
invitations_controller.rb
def create
@invitation = Invitation.new(invite_params)
if @invitation.save!
flash.now[:success] = "Invited!"
redirect_to root_path
else
flash.now[:error] = "Failure!"
redirect_to root_path
end
end
private
def invite_params
params.require(:invitation).permit(:attended_event_id, :attendee_id)
end
end
Do you mean something like this?
Do you want to achieve something like this:
There is a good basic example on RailsGuides
http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object