yerassyl
yerassyl

Reputation: 3048

Getting access to the form params in Rails

I have a form with hidden field:

<%= f.hidden_field(:equipment_id, :value => " ") %>

The value for this field is submitted via js. So that my generated html looks like this:

<input value="2" type="hidden" name="programm[equipment_id]" id="programm_equipment_id">

When i submit my form my parameters look like this:

"programm"=>{
  .. some other params ..,
 "equipment_id"=>"2"
}

In my controller's create method I try to assign equipment_id parameter to @programm.equipment_id:

@programm.equipment_id = params[:programm => :equipment_id ]

like that. Where equipment_id is an integer column in database. The problem is that nothing gets assigned. If i try to cast .to_i "0" is being assigned and stored in database. I also tried to do:

@programm.equipment_id = params[:equipment_id ]

But the problem is the same.

Upvotes: 0

Views: 58

Answers (3)

Milind
Milind

Reputation: 5112

for the view like this:-

<%= form_for(@programm) do |f|%>
  <%= f.hidden_field(:equipment_id, :value => " ") %>
<%end%>

Upon form submission the value entered by the user will be stored in params[:programm][:equipment_id] and can be used to access the value.

Upvotes: 0

This should do the trick:

@programm.equipment_id = params[:programm][:equipment_id]

Upvotes: 1

mgrim
mgrim

Reputation: 1302

The params are nested. You can access it like this:

@programm.equipment_id = params[:programm][:equipment_id]

Upvotes: 1

Related Questions