nohyg nohyg - 3 months ago 8
Ruby Question

rails - how can I receive data like 'name="temp[10]"' in controller by params

If I define

input name="temp[10]"
, How can I receive this input in controller?
Use params[]

<input type="checkbox" value="100" name="temp[10]"></input>

Answer

Try params['temp']['10'], it should do the trick.

Rails converts parameters with [] brackets into hashes. So your controller receives something like "temp"=>{"10"=>"100"}.

Such input:

<input type="checkbox" value="100" name="temp[10]"></input>
<input type="checkbox" value="value for 20" name="temp[20]"></input>
<input type="checkbox" value="value for 30" name="temp[30]"></input>

Will result in the following controller parameters (if everything was checked):

"temp"=>{"10"=>"100", "20"=>"value for 20", "30"=>"value for 30"}

So accessing it:

params['temp']['10'] # holds "100"
params['temp']['20'] # holds "value for 20"
Comments