GeekFitness GeekFitness - 3 months ago 9
Ruby Question

Passing parameters on button click in Rails

Right now, I have it so that when users pick an option from

collection_select
, a div is populated based on the
event_id
of their choice. Depending on the
Event
, there will be multiple
EventOption
shown, each with their own unique ids. (I have
EventOption
as
belongs_to
Event
, each of which
has_many
of them.)

This is what I have populating via JS/Ajax when they select an
Event
:

<div class="center" >
<h2>Available Options for "<%= @event.name %>":</h2>
<% @event.event_options.each do |e| %>
<p><strong>Name:</strong> <%= e.name %> </p>
<p><strong>Description:</strong> <%= e.description %></p>
<p><strong>Price:</strong> <%= e.price %></p>
</div>

<div class = "center row">
<div class = "col-xs-6">
<%= link_to "Check Available Dates", root_path, :class => "button", :id => "available-dates-button" %>
</div>
<div class = "col-xs-6">
<%= link_to "Book This Event Now!", book_now_path(:event => @event.id), :id => "book-now-button", :class => "button" %>

</div>
<hr>
<% end %>
</div>


My issue is that I can pass the
event_id
when they click the
Book This Event Now!
, but that's not what I need. I need the specific
:id
of the
:event_option
to be passed over, and I don't know how to get it from the iterated
.each
I have it coming from.

How can I make sure that each iteration of a button that follows my
book_now_path
has a unique ID that I can use to complete their reservation?

Answer

Just add the event_option.id to your book_now_path helper.

book_now_path(:event => @event.id, :event_option_id => e.id)

Then in the controller action it will be in the params:

@event_option_id = params[:event_option_id]

You can add any additional parameters to the Rails routes helpers simply by adding their names and values as keys and values to the helper method's options.