rails select_tag的输出

时间:2011-06-19 20:54:42

标签: ruby-on-rails html-select helpermethods

我是rails的新手,我很难弄清楚如何在rails 3中使用select表单的输出。以下是我的select标签:

<%= 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值并将其分配给控件方法中的变量“create”,目前看起来像这样:

 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正在传递,如我触发的代码错误中所示。经验是否与以下相关:订单?

{"commit"=>"Get Started!",
 "authenticity_token"=>"LrE2oOk2AkoUJbddC2crjnA5j4tIdxLGla52LWISx08=",
 "utf8"=>"✓",
 "order"=>{"material_id"=>"1",
 "frame_id"=>"1"},
 "experience_id"=>"4"}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

这个怎么样:

<%= select_tag(:experience, 
               Experience.all.collect {|e| [ e.experience_type, e.id ] } ) %>

也许这会对你有所帮助:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

答案 1 :(得分:-1)

解决了它 - select标签没有将experiernce_id作为:order参数返回,就像它自己的参数(experience_id =“6”)一样,而不是:order {experience_id =“6”,material_id =“ 1" }。解决方案只是改变选择标签:

<%= f.select :experience_id,    options_from_collection_for_select(@experiences, :experience_id, :experience_type) %>

f.select确保将值作为订单参数返回,并选择:experience_id返回和可以插入“experience_id”字段下的订单表中的id值。

希望这可以帮助其他新手使用rails表单或一般表单。