我是 Rails 新手,很难弄清楚如何在 Rails 3 中使用选择表单的输出。以下是我的选择标签:
<%= form_for(:order, :url => {:action => 'create_order'}) do |f| %>
<%= select_tag( "experience_id", options_from_collection_for_select(@experiences, :experience_id, :experience_type)) %>
<% frame = frame.id %>
<% material = material.id %>
<%= f.hidden_field :frame_id, :value => frame %>
<%= f.hidden_field :material_id, :value => material %>
<div class="submit-button">
<%= submit_tag("Get Started!") %>
</div>
脱离上下文可能有点令人困惑,但需要获取体验 ID 值并将其分配给“创建”的控制器方法中的一个变量,目前看起来像这样:
def create_order
#need to assign the submitted select_tag value and assign it to @order.experience_id
@order = Order.new(params[:order]).update_attributes(params[:order])
@order_id = Order.last.id
redirect_to(:action => 'drivetrain', :id => @order_id )
end
并且体验 id 正在通过,如我触发的代码错误所示。experience_id 与 :order 没有关联吗?
{"commit"=>"Get Started!",
"authenticity_token"=>"LrE2oOk2AkoUJbddC2crjnA5j4tIdxLGla52LWISx08=",
"utf8"=>"✓",
"order"=>{"material_id"=>"1",
"frame_id"=>"1"},
"experience_id"=>"4"}
谢谢你的帮助!