how do you access params that are both model based and non model based?

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>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?

Many thanks

Jonathan Gill

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