Ruby on rails 在Rails中的按钮单击上传递参数
现在,我有了它,当用户从Ruby on rails 在Rails中的按钮单击上传递参数,ruby-on-rails,ruby,model-view-controller,parameter-passing,ruby-on-rails-5,Ruby On Rails,Ruby,Model View Controller,Parameter Passing,Ruby On Rails 5,现在,我有了它,当用户从collection\u select中选择一个选项时,就会根据他们选择的event\u id填充一个div。根据事件,将显示多个事件选项,每个选项都有自己独特的ID。(我有Event选项,因为属于事件,每个都有许多。) 这就是当他们选择事件时,我通过JS/Ajax填充的内容: <div class="center" > <h2>Available Options for "<%= @event.name %>":</h2&g
collection\u select
中选择一个选项时,就会根据他们选择的event\u id
填充一个div。根据事件
,将显示多个事件选项
,每个选项都有自己独特的ID。(我有Event选项
,因为属于事件
,每个都有许多。)
这就是当他们选择事件时,我通过JS/Ajax填充的内容:
<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>
“”的可用选项:
名称:
说明:
价格:
“按钮”,:id=>“可用日期按钮”%>
@event.id),:id=>“立即预订按钮”,:class=>“按钮”%>
我的问题是,当他们单击立即预订此活动时,我可以传递事件\u id
代码>,但这不是我需要的。我需要传递:event\u选项
的特定:id
,我不知道如何从迭代的中获取它。每个我都有它
如何确保我的book\u now\u路径
后面的按钮的每次迭代都有一个唯一的ID,我可以用它来完成他们的预订?只需将事件选项.ID
添加到您的book\u now\u路径
助手中即可
book_now_path(:event => @event.id, :event_option_id => e.id)
然后在控制器操作中,它将在参数中:
@event_option_id = params[:event_option_id]
只需将Rails routes辅助对象的名称和值作为键和值添加到辅助对象方法的选项中,即可将任何其他参数添加到Rails routes辅助对象中 这太有道理了!非常感谢。我可以将@event\u option\u id传递到我的表单中,并调用我需要的其他列,我想?@GeekFitness,我想你指的是book\u now
操作视图中的表单,所以是的。在book\u now.html.erb
中测试@event\u option\u id
时,我一直没有得到任何值。它在我的服务器日志中显示为参数:{“event”=>“4”,“event\u option\u id”=>“5”}
,但当我尝试使用
时,它返回为空,当我刚刚测试它为时,它显示为0
,而不是5
。有什么想法吗?@GeekFitness,您确定要将参数分配给@event\u option\u id
,而不是event\u option\u id
。否则,这真的是一个新问题。如果你想打开另一个问题并发布你的代码,我来看看。是的,它使用的是@event\u option\u id
。这是我的第一个想法。我发布了一个新问题,并详细阐述了一点。非常感谢你!