Jonathan Gill wrote:
> Hi all
>
> Ive got a form thats based on a model that I need to put a drop down
> box and additional text field to build the description for the item.
>
> heres some quick code
>
> View
>
> <%= form_for (@shape) do |f| %>
> <%= f.text_field :description %>
> <%= select_tag "shape_name", "<option>round</option><option>square</
> option><option>triangular</option>" %>
> <%= text_field_tag "shape_code" =>
>
>
> Controller
>
> @shape.new
> @shape.description = params[:description]
> @shape.shape = "#{params[:shape_name]} #{params[:shape_code]}"
> @shape.save
>
> What happens is I get the shape.shape set right (to the name from the
> select box and the code from the text field) but description is always
> empty,
>
> Heres whats in the params when I do a raise shape.to_yaml to debug it.
>
> {"shape_name"=>"triangular",
> "shape_code"=>"shape code is here",
> "order"=>{"description"=>"this is a description"}
> "shape_code"=>"342",
> "shape_number"=>"111",
> "finish1"=>"Silver",
> "commit"=>"Create",
>
>
> Can someone point out how I should be accessing the params so I can
> set the description of the shape?
>
Because you're using form_for it prefixes the form element names with
the model name. Which in your case must not be Shape, but Order, seeing
that params[:order][:description] contains the description.
That is how you access the value by the way:
params[:order][:description]
Honestly though, I'm really not sure why the description is going into
params[:order], is Order really the model name? I'm going to assume
so...
Here's a brief, potentially correct explanation of how this all works.
<input name="order[description]" ... />
This gets pulled into the params hash as params[:shape][:description].
Following this naming convention for your form items (or using f.select
and f.text_field with form_for) will give you the ability to use this
syntax:
@order = Order.new(params[:order])
@order.save
Best,
Michael Guterl
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---