在Rails中单击按钮时传递参数 [英] Passing parameters on button click in Rails
问题描述
现在,我有了它,以便当用户从collection_select
中选择一个选项时,将根据其选择的event_id
填充div.根据Event
,将显示多个EventOption
,每个都有各自的唯一ID. (我有EventOption
作为belongs_to
Event
,每个都有has_many
.)
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.)
这是我在选择Event
时通过JS/Ajax填充的内容:
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>
我的问题是,当他们单击Book This Event Now!
时,我可以通过event_id
,但这不是我所需要的.我需要传递:event_option
的特定:id
,并且我不知道如何从迭代的.each
中获取它.
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.
如何确保book_now_path
之后的按钮的每次迭代都具有唯一的ID,我可以使用该ID来完成预订?
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?
推荐答案
只需将event_option.id
添加到您的book_now_path
帮助器中.
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]
只需将其名称和值作为键和值添加到helper方法的选项中,就可以向Rails路由助手添加任何其他参数.
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.
这篇关于在Rails中单击按钮时传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!