ruby on rails - Handling differents parameters in the same form with the same model -


let's have problem , restriction model class. problem has many restrictions , accepts nested attributes restrictions. restriction model represents mathematical expression like:

  • x < 1
  • 1 <= x <= 2
  • 2 < x

when create problem through form must create restrictions (always 3).

for restriction form fill numbers. i.e: x < [input number] note second restriction need 2 fields fill first , third need one. how can create each restriction in problem controller differents params? first , third restriction need pass number second need pass 2 numbers (maybe , array 2 numbers)

please, let me know if i'm not clear.

you can set name of inputs empty array:

number_field_tag 'problem[restrictions][1][]', 0 number_field_tag 'problem[restrictions][1][]', 5 

and in problemcontroller should receive params this:

params[:problem][:restrictions][1] # => contains 2 values serialized array 

so full form be:

x <  <%= number_field_tag 'problem[restrictions][0]', 0 %>  <%= number_field_tag 'problem[restrictions][1][]', 0 %> <= x <= <%= number_field_tag 'problem[restrictions][1][]', 5 %>  <%= number_field_tag 'problem[restrictions][2]', 0 %> < x 

and end following params:

params = {   problem: { #all problem attributes filled in form },   restrictions:    [     <value of first input>,     [<value of second input>, <value of third input>],     <value of fourth input>   ] } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -