What's the simplest way in Ruby-on-Rails to create several simple hidden fields with known values and the same name in a number of non-model forms (form_remote_tag in my case, but I'm guessing that isn't relevant)?
By "simple hidden field", I mean one where the name is just a single string (field_name) rather than part of an array (field_name[]), so that the value can be read simply from the params hash as params[:field_name] rather than params[:field_name][0].
I have found that
<% form_remote_tag :url => {:action => "do_act"} do %>
<%= hidden_field :field_name, 0, :name => "field_name", :value => "foo" %>
<%= submit_tag "Submit" %>
<% end %>
produces an acceptable element (<input id="field_name_0" name="field_name" type="hidden" value="foo" />), but if I omit the :name parameter then the rendered field has the name field_name[0]. Omitting the 0 obviously causes really odd behaviour.
<%= hidden_field_tag :field_name, "foo" %> produces an acceptable element if there's only one such form, but generates HTML warnings (duplicate IDs) if there are more than one.
Is there a way to do this (barring defining a helper) in fewer arguments?
I would use hidden_field_tag and set the ID manually based on some value that is different for each form. Like this:
<%= hidden_field_tag :field_name, 'value', :id => 'field_name_' + unique_value %>
Where unique_value can be anything at all. If these forms have some sort of parent record that they refer to, it could be the ID of the parent. I assume that's why you have multiple similar forms on the same page in the first place.
You can simple pass the ID as an option. The method (form_tag_helper.rb) is defined as:
def hidden_field_tag(name, value = nil, options = {})
text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
end
So writing:
<%= hidden_field_tag :field_name, "foo", :id => "hidden_field_1" %>
<%= hidden_field_tag :field_name, "bar", :id => "hidden_field_2" %>
Produces:
<input id="hidden_field_1" name="field_name" type="hidden" value="foo" />
<input id="hidden_field_2" name="field_name" type="hidden" value="bar" />
Try hidden_field_tag:
<%= hidden_field_tag :field_name, "foo" %>
Related
I am passing a hidden field for a form to distinguish between views the request came from:
<%= hidden_field_tag("advanced", true)%>
Apparently true gets passed as a string. I tried different syntaxes like:
<%= hidden_field_tag "advanced", true %>
<%= hidden_field_tag "advanced" => true %>
<%= hidden_field_tag :advanced => true %>
It always gets translated to this
<input type="hidden" name="advanced" id="advanced" value="true" />
Obviously, I can check the string value in the controller, but is this the expected behaviour?
Rails translate your parameters to the equivalent html, and it is only possible to have strings in html. The hidden_field_tag is just a hidden text_field_tag, so the value need to be text. And in the url you also have a string. So the conversion need to be done in the controller.
I have a form for some model, inside this form I have some text_fields and hidden_fields
that i need to use in the controller but that are not from the model.
this is a simplified version of it
<%= from_for #user do |f| %>
<%= f.text_field :name %>
<%= hidden_field :photo, value: 'blabla' %>
<%= text_field :type %>
<% f.submit %>
Lets say that the :photo and :type parameters are not in the model user but i need them to decide how to create the user.
they are going in the params hash, but all messed up. How do I access their value?
Thank you
hidden_field_tag "photo", "photo_value"
=> <input id="photo" name="photo" type="hidden" value="photo_value" />
Then in your controller:
#hidden_photo = params[:photo]
Whenever you are working with a form and want a value not associated with a model or object, then use the helpers ending in "*_tag"
I am trying to create a form which loads upon a user clicking a date in a calendar, the form then is passed the date that is clicked through the URL and the controller assigns that date to the #date variable. I then create a date_select element and assign it the #date variable. This works fine but since I do not want the user to be able to edit the date in the form I want it to be hidden.
I pass these html options to the form but it doesn't seem to ever effect the HTML:
<%= f.date_select :date, :default => #date, :type => "hidden" %>
Am I missing something? I also tried passing it in an HTML hash :html => { :type = "hidden" } but that doesn't work either. Even when I try something different like :class => "something" it doesn't change the HTML. Is there something special about the date_select helper?
date_select accepts the options discard_day, discard_month and discard_year to do exactly what you are trying to achieve.
<%= f.date_select :date, { :discard_day => true, :discard_month => true, :discard_year => true } %>
Behind the scenes, it generates the following HTML code:
<input id="record_date_3i" name="record[date(3i)]" type="hidden" value="5" />
<input id="record_date_2i" name="record[date(2i)]" type="hidden" value="1" />
<input id="record_date_1i" name="record[date(1i)]" type="hidden" value="2012" />
No CSS tricks, no changes in your controllers.
Per the name, date_select generates <select> elements. In no version of (X)HTML does the select element support the type attribute. If you want a hidden form field then you should use the hidden_field helper, which generates <input type="hidden"> elements.
(To answer your implied question about using e.g. :class => 'something', the problem is that the options and html_arguments parameters must be two separate hashes, but if you do something like this:
<%= f.date_select :date, :default => #date, :class => 'something' %>
..the Ruby interpreter assumes that you have supplied a single hash, i.e. { :default => #date, :class => 'something' } (and really, can you blame it?), and since class isn't a valid key for the options hash it's ignored. Instead you have to make it obvious to Ruby that these are two separate parameters by doing something like this instead:
<%= f.date_select :date, :default => #date, { :class => 'something' } %>
<%# Hey Ruby, this is a different Hash! ----^ %>
See the difference? Of course you could go bonkers and be really obvious, e.g.:
<%= f.date_select(:date, { :default => #date }, { :class => 'something' }) %>
..but that's ugly and egregious so don't bother.)
You can put it inside a hidden div:
<div style="display: none;">
<%= f.date_select :date, :default => #date, :type => "hidden" %>
</div>
That will allow you to have all the fields and hidden you can also use for date and time select:
<div style="display: none;">
<%= f.datetime_select :date, :default => #date, :type => "hidden" %>
</div>
I would like to use the form_for helper multiple times for the same model in the same page. But the input fields use the same ID attribute (in the HTML), so clicking on the label of a field in another form will select the same input in the first form.
Is there a solution besides settings all attributes manually via :for => "title_#{item.id}" and :id => "title_#{item.id}"?
Using Rails 3.0.9
You can use :namespace => 'some_unique_prefix' option. In contrast to :index, this will not change the value used in the name attribute.
It's also possible to use an array, e.g. when you have nested forms or different forms that happen to have some fields in common: :namespace => [#product.id, tag.id] or :namespace => [:product, #product.id]
I found the answer myself, one can pass a :index option to form_for. That string will be used in the id and for attributes:
<%= form_for #person, :index => #person.id do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
will parse
<form accept-charset="UTF-8" action="/person/11" class="edit_person" id="edit_person_11" method="post">
<!-- Hidden div for csrf removed -->
<label for="person_11_name">Name</label>
<input id="person_11_name" name="person[11][name]" size="30" type="text" />
<input name="commit" type="submit" value="Update Person" />
</form>
Notice it'll change the name of the inputs as well.
I believe you can add this param:
:html => { :id => 'id_i_want' }
I got a list page and I filter items via links with get params (I can choose many links so query would be like "?param1=value1¶m2=value2"). But also I have to filter it by text field, so I made a form:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
But when I submit it, text field param replaces existing query params. So, how to make text field value add to query, not to replace it?
Since I was just dealing with this problem in Rails 4 I thought I'd share my solution.
My page gets loaded with a sport_id parameter, and when the user specifies a sort-order I wanted it to submit a GET request for page.url/event?sport_id=1&sortby=viewers but it wouldn't preserve the sport_id parameter until I added a hidden field tag in the form like so:
<%= hidden_field_tag :sport_id, params[:sport_id] %>
This solution does submit an empty sport_id parameter if that parameter was not in the original request, but that is easily prevented by encapsulating the hidden field in an <% if params[:sport_id].present? %> condition.
Use hidden_field_tag.
Inside of your form, just set hidden_field_tags for the existing GET params, like so:
<% request.query_parameters.collect do |key, value| %>
<%= hidden_field_tag key, value %>
<% end %>
This will ensure that your existing params persist.
Rails 3?
<%= form_tag your_path(params.except(:controller, :action)), :method => :get do %>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
<% end %>