0

私はレールに不慣れで、レール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"}

助けてくれてありがとう!

4

2 に答える 2

0

これはどう:

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

多分これはあなたを助けるでしょう: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

于 2011-06-19T21:04:39.130 に答える
0

それを解決しました-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 値を選択しました。

これがレールフォームや一般的なフォームに慣れていない他の誰かに役立つことを願っています.

于 2011-06-20T15:36:16.340 に答える