0

I have a hidden field in a form_for, where I have it set up like this:

  = f.hidden_field :building_id, :value => @building

Where @building is set in the controller in the new action:

@building = params[:building][:building_id]

When validation fails, the create action gets to this line:

format.html { render action: "new" }

and the page is rendered, the hidden_field is not getting it's value.

According to one of the answers to this question, it looks like maybe I should have this line in my form_for:

  = f.hidden_field :building_id, params[:building][building_id]

but I get an error when I do this. What am I doing wrong?

Community
  • 1
  • 1
John
  • 13,125
  • 14
  • 52
  • 73

1 Answers1

2

It's because the params[:building][:building_id] is no longer set when you load the page after a validation error occurs, as it's now stored in params[:building_id]. Something like @building = params[:building_id] || params[:building][:building_id] should work fine.

Zachary Anker
  • 4,500
  • 21
  • 19
  • Right idea! I had to set the parameters in the create action of the controller, such as @building = [:][:building_id]. – John Dec 29 '11 at 01:38
  • Ah sorry yes, wasn't thinking about the fact that it was setup to call from a form already, not just a hidden_field_tag. – Zachary Anker Dec 29 '11 at 01:59