preciz
preciz

Reputation: 3

Wrapping something in the params hash

I would like to nest params with my rails form this way, ( An already started project, and this looks the easiest way, sorry if its not straightforward).

= form_for @building do |f|
  ...
  = fields_for :building_floors do
    - @building.floors.each_with_index do |building_floor, index|
      - fields_name = "building_floor_" + index.to_s
      = f.fields_for fields_name, building_floor do |bf|
        ...

So I would like to get a params hash like:

... "building_floors"=>{ "building_floor_1"=>{...}, "building_floor_2"=>{...}  }...

The question is: How can you wrap something in the params hash with rails.

Upvotes: 0

Views: 227

Answers (1)

lukaszkorecki
lukaszkorecki

Reputation: 1773

You can use nested from data.

Plain html example:

<form action="....">
  <input type="text" value="" name="floor[1]" />
  <input type="text" value="" name="floor[2]" />
</form>

So in your case that would be something like:

= form_for @building do |f|
  ...
  = fields_for :building_floors do
    - @building.floors.each_with_index do |building_floor, index|
      - field_name = "building_floor[#{index}]"
      = f.fields_for field_name, building_floor do |bf|
        ...

Upvotes: 1

Related Questions