Trying to format a Datetime select helper by passing in a class like so has not been working. I at first thought Bootstrap might be wrong, but after looking at the source, 'span2' is not there.
<%= f.datetime_select :arrival_nor_tendered, class: "span2" %>
This seems like the headslap way to do it but I may be wrong.
Try this:
<%= f.datetime_select :arrival_nor_tendered, {}, { class: "span2" } %>
The last parameter is html_options but it has a hash parameter before it and ruby may missinterpret the two.
Related
Book.extended_fields = {}
What is the select version of
<%= text_field_tag("book[extended_fields][description]", nil, class: "form-control") %>
?
I'm looking to create something like
<select class="form-control" name="book[extended_fields][description]"></select>
This is for a partial that will render a form that uses dynamically created fields. So, ultimately, it'll be in a loop that's something like:
<%= text_field_tag("book[extended_fields][#{k}]", nil, class: "form-control") %>
where k represents an item in a collection of fields to be created.
To answer your main question, select_tag creates the html select option you're looking for. You can find the documentation here.
Correct structure should be laid out as such
select_tag(name, option_tags = nil, options = {}, {}, {:class => 'form-control'})
Let me know if I misunderstood your question.
I was fighting rails instead of using it. I had been trying to use the select helper and couldn't figure out how to use multiple arguments for the name portion that then get concatenated into the attribute. The answer was simply to define the name attribute myself.
<%= select("book", "description", (options), {include_blank: true}, class: "form-control", name: "book[extended_fields][description]") %>
The documentation for Rails select form helper states (see documentation):
select(object, method, choices = nil, options = {}, html_options = {}, &block)
Which allows adding a class simple, like so:
<%= f.select :some_attr, MYOPTIONS, {}, {class: 'my-class'} %>
My question is, how do I add a class to it when using it as a block? Rails documentation states:
select(report, "campaign_ids") do
available_campaigns.each do |c|
content_tag(:option, c.name, value: c.id, data: { tags: c.tags.to_json })
end
end
It doesn't work when I use it like so:
<%= f.select :some_attr, {}, {class: 'my-class'} do %>
<% MYOPTIONS.each do |MYOPTION| do %>
<%= content_tag :option, MYOPTION.label, value: MYOPTION.value %>
<% end %>
<% end %>
Nor does it work if I use:
f.select :some_attr, class: 'my-class' do
The class is not applied to the select tag in the HTML.
I solved my own problem, although I don't fully understand the answer, so if someone else understands this better, I'd love to hear your answer.
To get it to work, I simply added an additional empty hash to the beginning, like so:
<%= f.select :some_attr, {}, {}, {class: 'my-class'} do %>
<% MYOPTIONS.each do |MYOPTION| do %>
<%= content_tag :option, MYOPTION.label, value: MYOPTION.value %>
<% end %>
<% end %>
The second hash is still options and the last is still html_options, so as an example, you can also add include_blank like so:
f.select :some_attr, {}, {include_blank: true}, {class: 'my-class'}
However, I don't know what the first hash is, nor what values can be passed there. I've looked at the Rails source, but I still have no clue. If you have insight into this, I'd love to hear it.
A couple oddities to be aware of:
In your example, you're using f.select, which you can find a reference for here:
https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
Only the first parameters is required, the rest have defaults. However, to assign that HMTL class, you had to have a value for the fourth parameter, which necessitated having something for the second and third parameters as well.
What you ended up with is a valid solution:
<%= f.select :some_attr, {}, {}, {class: 'my-class'} do %>
<% MYOPTIONS.each do |MYOPTION| do %>
<%= content_tag :option, MYOPTION.label, value: MYOPTION.value %>
<% end %>
<% end %>
The block, when provided, takes precedence over the literal value (an empty hash in this case).
Surprisingly, if you were rendering this tag using select_tag instead of f.select, passing a block wouldn't be an option:
https://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
how can we define id for this rails select statement , i have tried doing in this way like
<%= f.select :state, options_for_select(Contact::STATES), :id=>"state_job" %>
but it is not showing any id when i inspect it in the browser. Please help me out
<%= f.select :state, options_for_select(Contact::STATES) %>
The select tag helper looks for options, then html_options, you just need to make sure your id is in the right place (html_options) by passing something to the options parameter:
<%= f.select :state, options_for_select(Contact::STATES), {}, {:id=>"state_job"} %>
I need to add a class to my select.It seems to be easy but I'm not able to figure it out.
I'm trying this but the class doesn't show up:
<%= f.select :dr_state, us_states ,:selected=>cr_dovi.try(:dr_state),:class=>"dr_state" %>
When I don't need to make that select auto-select It works in this way:
<%= f.select :dr_state, us_states ,{},:class=>"dr_state" %>
I have been trying to figure it out for about an hour please help me.
Try the following, I think Ruby may be parsing both of your final parameters into the same hash:
<%= f.select :dr_state, us_states , { :selected=>cr_dovi.try(:dr_state) }, :class=>"dr_state" %>
I'm in the middle of trying to create a form where one of the questions is to choose a specific language. I'm trying to use the language list gem here: https://github.com/scsmith/language_list . However, the documentation doesn't really show me how I would combine the list with a select_tag.
<%= form_for users_path, :method => "get" do %>
<%= label_tag "Select Country" %> <br>
<%= country_select(:user, :country, [], :include_blank => true) %> <br>
<%= label_tag "Language spoken" %>
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "id", "name") %>
<%= label_tag "City" %> <br>
<%= text_field_tag(:city) %>
<% end %>
gives me the error
undefined method 'id' for afr (af) - Afrikaans:LanguageList::LanguageInfo
on the line
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "id", "name") %>
Can anyone help me?
Don't use "id". The docs say you should use iso_639_1 (or iso_639_3 if you want 3-letter codes)
<%= select_tag "Language", options_from_collection_for_select(LanguageList::COMMON_LANGUAGES, "iso_639_1", "name") %>
The LanguageList class seems to return a hash of LanguageInfo instances, having attributes like name, type and code -- `options_from_collection_for_select' expects its first parameter to be the collection (the hash in this case), the second a method that will return the value you want to identify the item, and the third is a display string.
So when someone selects a language, what are you going to store in the database? Probably one of the codes, right? So in this were true, you would make the second argument a method that an instance of the collection would respond to, which (reading the source code of the gem) is either iso_639_1 or iso_639_3. name should already work.
So if you replace id with one of those two iso_nnn_n values, then the form should display. To actually save the language code in the database, you'll need a column in your database for it, which you may already have as language.
Sometimes it's make a lot of sense to store gem's data dump in the database.
here is sample with postgresql and rails https://github.com/serghei-topor/import-language-list-into-db-rails-sample
here is csv file of gem's data dump https://github.com/serghei-topor/language-list-csv
And select_tag will look like:
<%= select_tag "Language", options_from_collection_for_select(Language.where(is_common:true).order(:name), "id", "name") %>