14

My attempt to place a hidden_field within a form_for is crashing within cucumber on an ActionView helper error. Something also about FixNum which escapes me since I haven't dug through the source code. My prices_controller shows this:

 @price = Price.new
  @commodity = Commodity.find(params[:id])

I want to make the link between price and commodity with this hidden_field:

 <%= form_for (@price), :url => prices_path  do |f| %>
  <% f.hidden_field :commodity_id, @commodity.id %>
 .
 .
 <div class="actions">
 <%= f.submit "Submit" %>
   </div>

Looked at the form_for api and the above should work. Reading other replies on stackoveflow, I have put the hidden_field in its own div within the form, added a Hidden_field_tag, and placed it within the actions div before the submit line. Looking at the merge msg, I guess it doesn't like something about the line, but it appears OK to me. The commodity_id field is the match field, sam

sam452
  • 1,281
  • 3
  • 20
  • 33

1 Answers1

24

If you could paste the error message itself, and the relevant lines of the trace, it could help us. Right now, the only thing I see is that the ERB tag before f.hidden_field should be <%=, and I'm not sure about it since I don't use ERB. For what it's worth, merge is usually used with Hash objects. Maybe it can point you in the right direction

EDIT Ok I get it. You have to write f.hidden_field :commodity_id, :value => @commodity.id.

ksol
  • 11,835
  • 5
  • 37
  • 64
  • undefined method `merge' for 111:Fixnum (ActionView::Template::Error) /Users/sam/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.1.3/lib/action_view/helpers/form_helper.rb:1348:in `objectify_options' /Users/sam/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.1.3/lib/action_view/helpers/form_helper.rb:1301:in `hidden_field' and I just added the "=" to the erb language to try that. It seems to have failed as well. thx. – sam452 Feb 14 '12 at 15:19
  • Yes, it works now. Should've been literal with the api example. THX – sam452 Feb 14 '12 at 15:26
  • You're welcome. It's tricky to remember it sometimes, since there is a `form_builder` version, a `form_helper` one, and the `*_tag` one – ksol Feb 14 '12 at 15:30