Search code examples
ruby-on-railshtml-selecthelpermethods

output of the rails select_tag


i'm new to rails and am having difficulty figuring out how to use the output of a select form in rails 3. the following is my select tag:

<%= 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>

it might be a little confusing out of context but ineed to get the experience id value and assign it to a variable in the controller method for 'create' which currently looks like this:

 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 

and the experience id is getting passed through as seen in a code error i triggered. is the experience_id not getting associated with :order?

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

thanks for any help!


Solution

  • solved it -- the select tag wasn't returning the experiernce_id as an :order parameter, just as it's own parameter (experience_id = "6") for example and not :order { experience_id = "6", material_id = "1"}. the solution was just to change select tags:

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

    f.select ensured that the value was returned as an order parameter and selecting for the :experience_id returned and id value that could be inserted into the order table under the "experience_id" field.

    hope this helps someone else new to rails forms, or forms in general.