I have the following select_tag and would like to have the labels translated. I am using Rails Internationalization (I18n) API
Now, this code works how ever I need to translate the option label.
<%= select_tag "object",
"<option value=address>Address</option>
<option value=Orden>Orden</option>".html_safe %>
I have tried:
<%= select_tag "object",
"<option value=address><%= t('address').capitalize %></option>
<option value=Orden>Orden</option>".html_safe %>
But I get a syntax error. I have also tried:
<%= select_tag "object",
"<option value=address> t('address').capitalize </option>
<option value=Orden>Orden</option>".html_safe %>
The last one will not translate and just put the string 't('address').capitalize'
Any advice?
You should look into options_for_select(args)
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
options_for_select([[t('address'), "address"], ["orden", "orden"]], params[:the_selected_one])
you would do:
<%= select_tag("object", options_for_select([[t('address'), "address"], ["orden", "orden"]], param[:the_selected_one])) %>
Try using options_for_select instead raw HTML options.
Check code below.
select_tag "object", options_for_select([t('address').capitalize, "address"], [t('order').capitalize, "order"])
Also you can use a Model to build the Select options. Please, check the documentation below for more information.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
Related
I have a form without a model backing it built using form_with in Rails 6:
<%= f.text_field :one %>
<%= f.select :two, [['Option 1',1],['Option 2',2]] %>
<%= f.submit 'Submit' %>
The only documentation I can find to set which of the select options are selected by default says that it will pre-select whatever is in the model. Since I don't have a backing model, how can I choose which option is selected? I've poked around the options a little and found nothing, but I do not necessarily know where to look.
You must have missed it, there's an optional selected keyword argument.
Lastly, we can specify a default choice for the select box with the
:selected argument:
<%= form.select :city, [["Berlin", "BE"], ["Chicago", "CHI"], ["Madrid", "MD"]], selected: "CHI" %>
Output:
<select name="city" id="city">
<option value="BE">Berlin</option>
<option value="CHI" selected="selected">Chicago</option>
<option value="MD">Madrid</option>
</select>
https://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
There're many ways to create a drop down in Rails. And it's not clear why there're so many and which one is idiomatic.
I'm using Rails 5x and the built-in form helper.
<%= form_for(#my_model, :url => some_url(#my_model)) do |f| %>
<%= f. # ?? f.select ?? f.collection_select ?? or anything else?
What's the idiomatic and recommended way?
Note that I don't want to use an external gem such as simple_form.
Select - for basic usage.
For example, in this case you need to add value manually:
<%= f.select(:name, :some_id, [['Emma', 1], ['Oliver', 2]) %>
But if you have model with data for this value, you can use collection_select:
<%= collection_select(:something, :id, User.all, :some_id, :name) %>
Update
Example for your comment:
<%= options_for_select(
[
['Emma', 1, { 'data-attr' => '200ms' }],
['Oliver', 2, { 'data-attr' => '400ms' }]
], 2 # selected options id
) %>
HTML Result:
<option value="1" data-attr="200ms">Emma</option>
<option value="2" selected="selected" data-attr="400ms">Oliver</option>
collection_select is intended to be used when the list of items is an array of ActiveRecord objects. collection_select is built on the top of select so it's a convenient method when you need to display a collection of objects and not an array of strings.
You may refer here for detailed answer.
I am using bellow code and function options_from_collection_for_select for generating options for user.
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'email') %>
Above code generate bellow html:
<select id="receiver" name="receiver" style="display: none;">
<option value="1">email1#yahoo.com</option>
<option value="2">email2#gmail.com</option>
<option value="3">email3#gmail.com</option>
</select>
But i want email with username, e.g <optionvalue="1">email1#yahoo.com(some_user)</option>
Suggestion any alternate function or customize current function will be appreciated.
In your User model add a method :
def user_dispay_name
"#{email}(#{full_name})"
end
Now do :
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'user_dispay_name') %>
But without options_from_collection_for_select this function i'm using below code:
<%= select_tag 'receiver', options_for_select(#user.map{ |c| ["#{c.email} (#{c.display_name})", "#{c.id}"] }) %>
I have the following form code
<%= f.fields_for resource.paid_account do |pa| %>
<%= pa.collection_select :account_plan_id, #account_plans, :id, :name_with_price %>
<% end %>
that generates the following HTML
<select id="user_paid_account_account_plan_id" name="user[paid_account][account_plan_id]">
<option value="2">Lite ($10.00/mo)</option>
<option value="3">Professional ($20.00/mo)</option>
<option value="4">Plus ($30.00/mo)</option>
</select>
Is user[paid_account][account_plan_id] the right name? Shouldn't it be user[paid_account_attributes][account_plan_id]?
I ask because this is causing problems on the backend; my account_plan record isn't getting created.
Looks like this answer is yes. I manually changed the name like this:
<%= pa.collection_select :account_plan_id, #account_plans, :id, :name_with_price, {},
{ name: "user[paid_account_attributes][account_plan_id]" } %>
and now it works. Feels like a hack, though, so it seems like there must be a better way to do it.
I currently have my index.html.erb showing the following code.
<select name="country">
<option>All</option>
<%= country_options_for_select('All') %>
</select>
But the result of the page becomes like this in the html source:
<select name="country">
<option>All</option>
<optionvalue="Afghanistan">Afghanistan</option><optionvalue="Aland
Islands">AlandIslands</option> ...
</select>
It should be instead of <option>
What did I do wrong?
Try using select_tag instead. It looks a bit cleaner.
<%= select_tag "name", country_options_for_select() %>
The reason the country options are showing up incorrectly is because you are passing 'All' to it. It doesn't need an argument there in your case. Only if you wanted a certain option selected by default.
For example,
<%= select_tag "name", country_options_for_select('Chile') %>
More info on it's use here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/country_options_for_select
In Rails 2.3.14 to Rails 3.1.0, this works:
<%= country_options_for_select.html_safe %>