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>
Related
I am trying to implement bootstrap multiselect field in my rails app.
Making use of the bootstrap-multiselect_rails gem found here (https://github.com/TrevorS/bootstrap-multiselect_rails)
Have it installed and configured successfully but in my form am not able to select multiple vales. It allows me to select only an single value.
Right now my code looks like this:
<%= f.collection_select :role_pm, User.where(:user_role => 'Project Manager'), :name, :name, {}, {:multiple => 'true'}, {class: "role_pm"} %>
Where am I going wrong?
Finally got this working. I have Update the line of code in this answer which has caused me a lot of agony over the past 2 days or so
<%= f.collection_select :role_pm, User.where(:user_role => 'Project Manager'), :name, :name, {}, :multiple => 'true', :class => 'role_pm' %>
Looks like I have passed both multiple and class attributes as seperate arrays which was really not needed in the first place.
.You need to initialize Multiselect using the js.
here comes my working code:-
###HTML FILE
##my controller has #event_types to autopopulate the values as well for edit action
<label for="events" class="control-label form-group col-md-12">Event Type: </label>
<div class="form_group col-md-12">
<div class="btn-group">
<%= select_tag("event_types", options_for_select(#event_types.pluck(:name),:multiple=>true,:required=>true) %>
</div>
</div>
###js FILE-initialise using id/class for multiselect
$('#event_types').multiselect({
enableFiltering: true,
filterBehavior: 'text',
enableCaseInsensitiveFiltering: true,
nonSelectedText: 'Select the type of events'
});
you can use selected attribute in select tag to select those values during edit action.Just pass it from controller..example
:selected => #event_types.new_record? ? nil : #event_types.pluck(:name)
rewriting your query...
<%= select_tag("event_types", options_for_select(#event_types.pluck(:name),:multiple=>true,:required=>true) %>
changes to :Showing names in select dropdown list
<%= select_tag("role_pm", options_for_select(User.where(:user_role => 'Project Manager').pluck(:name).uniq,:multiple=>true,:required=>true) %>
In my forms, users are displayed a specific view in a sequence depending on a value in a session. This is all working great except when users go back to a previous form, the form is not pre-filled with data from the session.
My model does not extend ActiveModel as I am not using local DB's for anything.
<%= form_tag do %>
<%= field_set_tag do %>
<label for="form_firstname">First name:</label>
<%= text_field "form", "firstname" %>
<label for="form_surname">Surname:</label>
<%= text_field "form", "surname" %>
<label for="form_dob_3i">Date of Birth:</label>
<%= date_select("form", "dob", :start_year => 1912, :end_year => 1994, :order => [:day, :month, :year], :prompt => { :day => 'Day', :month => 'Month', :year => 'Year' }) %>
# and many other fields including radio/check/text/select ...
# next / back buttons...
<% end %>
<% end %>
My question is how can I get the form to remember data from the session and pre-populate the field?
For text fields, I can add a value attribute like so, but I am not sure this is the best way to go about it:
<label for="form_address_line_1">Address:</label>
<%= text_field "form", "address_line_1", :value => session[:quote]['policyholder']['address_line_1'] %>
I am also not sure how to apply this to radio fields etc as the following would not work (although it makes sense to me):
<%= radio_button_tag 'form[homeowner]', 'yes', :checked => 'checked' if session[:quote]['policyholder']['homeowner'] = 'yes' %>
<label for="form_homeowner_yes">Yes</label>
<%= radio_button_tag 'form[homeowner]', 'no', :checked => 'checked' if session[:quote]['policyholder']['homeowner'] = 'no' %>
<label for="form_homeowner_no">no</label>
Use your back button in your view to render the previous form and not redirect to it...
so try this rather than redirect_to: render 'your_controller_action'
I want to put several forms generated with Rails simple_form on a one page, and operate on them with javascript. However simple_form generated same ids for respective inputs in forms. Thus I want to replace generated id with my own.
Now I have a line:
<%= f.input :id, :as => :hidden, :html => {:value => #question.id} %>
and HTML output (for #question.id=1):
<input id="question_id" class="hidden" type="hidden" value="1" name="question[id]">
and I want to get:
<input id="question_id_1" class="hidden" type="hidden" value="1" name="question[id]">
question_id_1 is just an example. I want to be able to choose this id.
I use Rails 3 and simple_form 1.5.
You're almost there.
The trick is in specifying the :input_html.
<%= f.input :id, :as => :hidden,
:input_html => {
:value => #question.id,
:id => "question_id_1"
} %>
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' }
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" %>