Using select form helper to fill a hash column - ruby-on-rails

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]") %>

Related

How can I list items in rails for options_for_select?

I'm using rails 4 and I'd like to list categories in a select drop down menu. How can I do that? I have a form that looks like so:
<%= f.select (:category_id),
options_for_select([
["Maths", 1],
["Physics", 2]
])
%>
but of course, the content has to be dynamic from database, so I tried the following:
options_for_select([
#categories.each do |c|
[c.title, c.id]
end
])
but that outputs #<Category:randomdigetshere> if I try to get the same output outside of that options_for_select it works and the title / id is being displayed as it should.
What's the right way of doing it?
You could do
<% categories_array = Category.all.map { |category| [category.title, category.id] } %>
<%= options_for_select(categories_array) %>
or
<%= options_from_collection_for_select(Category.all, :id, :title) %>
You can make it even shorter using collection_select
Assuming your #categories's format is:
#categories = Category.all
You can do:
options_for_select(#categories.map { |category| [category.title, category.id] })
I cannot tell you how many times I've looked this up and fat fingered my way around it. Here's my take on it which includes:
default text displayed in select drop down
a method call on the instance which merges first_name and last_name attributes
sets :selected so it is available when editing
sets a class (useful for simple_form integration)
select_tag(:doctor_id, options_for_select(["Select doctor"] + #doctors.map{ |doctor| [doctor.full_name, doctor.id] }, :selected => :doctor_id), :class => 'form-control')
Remember that this needs to be wrapped in outputting tags i.e. <%= and %>
What options_for_select does is take an array and format into the native html options tag format along with the selected attribute, etc. What's going on here is that we initialize the array with a element titled "Select doctor" and then we append to that an array of items which look like ["Bob Smith", 1]

rails erb form helper options_for_select :selected

I have an edit form in erb.
<%= form_for #animal do |f| %>
Within the code I have a select with options:
<%= f.select :gender, options_for_select([['Mare'], ['Stallion'], ['Gelding']], :selected => :gender) %>
However, the select is not showing the correct selected value.
What could I be doing wrong?
I can get it to work if I hardcode it but of course that is not a viable option.
In your code, your options_for_select() call sets the selected value to "gender" and does not attempt to use the value from your form object.
Please see the docs for options_for_select() for usage examples.
options_for_select(['Mare', 'Stallion', 'Gelding'], f.object.gender)
options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.gender)
Alternatively, you can do this, which will already use the gender() value for your form object:
<%= f.select :gender, ['Mare', 'Stallion', 'Gelding'] %>
By the way, if you are using :include_blank => true, this will set your current selection to blank even though the form "knows" what is selected.

Rails + Ransack - Drop Down List Collection?

I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist.
Example:
<%= collection_select(:expense, :project_id, Project.all,
:id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %>
in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern.
Also, my form is on a controller and view called "reports", I am not putting this search in the default controllers.
Thanks!
Seems that this will work.
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", #search.project_id_eq) %>
If anyone has another suggestion, would love to know it too.
To do this with an include_blank, put it outside of the parentheses:
ex:
<%= f.select :languages_id_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>
== UPDATE ==
better yet, use f.collection_select. This way after the user searches for something using the drop down, that option is preselected on the following page:
<%= form.collection_select :vendor_id_eq, Vendor.all, :id, :name, include_blank: true %>

Rails: how to use language list gem with select_tag in a form?

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") %>

Can you pass a class into a Rails datetime select helper?

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.

Resources