Reputation: 7314
I have a form_tag
that takes a code as input:
= form_tag({controller: "charges", action: "new"}, method: "get") do
= label_tag(:code, "Check code:")
= text_field_tag(:code)
= hidden_field :item, value: @item.id
= submit_tag("Search")
I want to have a hidden field that adds a certain value to the URL. I basically want to create the URL example.com/charges/new?item=1&code=mycode
where '1' is the item id and 'mycode' is the code entered by the user, but I can't get it to work.
Upvotes: 2
Views: 7056
Reputation: 106127
hidden_field
and hidden_field_tag
are not the same. You want the latter. hidden_field
works like text_field
and so requires an object (usually inside a form_for
). hidden_field_tag
, like text_field_tag
, does not. This is a pretty common "gotcha" in Rails.
Try this:
= hidden_field_tag "item", @item.id
P.S. If this form is for creating a new model object (from your code I'm guessing it's for creating a Charge object), you probably want to use form_for
instead. Something like this:
= form_for :charge do |f|
= f.label :code, "Check code:"
= f.text_field :code
= f.hidden_field :item_id, @item.id
= f.submit "Search"
This would probably simplify your controller a lot, since in your create
action you could just do Charge.create(params)
. I'm just guessing at what your actual models looks like, but it should give you the right idea. You should read through the form_for
docs for more information.
Upvotes: 7