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.
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
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 select box that was created using nested form:
<select name="product[shop_attributes][id]" id="product_shop_attributes_id">
<option value="23">KMART</option>
<option value="24">Super Shop</option>
<option selected="selected" value="22">TARGET</option>
<option value="new">Create New Shop</option>
</select>
selected="selected" was created by passing :selected => "22" to f.select options.
The problem is that no matter what option is selected, the submitted value is always "22".
I noticed that a hidden input is created, which I believe causes the problem:
<input type="hidden" value="22" name="product[shop_attributes][id]" id="product_shop_attributes_id">
Thus, there are 2 elements with id=product_shop_attributes_id.
What could cause to this hidden input field to be generated ?
Relevant code of select box creation:
<%= form_for #product do |f| %>
<%= f.fields_for :shop do |sf| %>
sf.select(:id, <options>, {:prompt => true, :selected => <default_value>})
<% end %>
<% end %>
Relevant controller code:
def edit
#product = Product.find(params[:id]) # the select box indeed gets it's initial value from #product
end
def update
#temp = params.inspect
end
update.html.erb:
<%= #temp %>
I see here always the same (no matter what option is selected):
"product"=>{"shop_attributes"=>{"id"=>"22"},...}
There's nothing wrong with the rails generated HTML. It's probably the way you're accessing it in your controller. Could you post the original rails code that generated this HTML and the code you are using to process it?
The problem is, as I mentioned in the question, the hidden input field with the same id as the select.
I opened a separate question to investigate why this happens.
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 %>
Today is the first day I'm looking into Ruby on Rails, and now I'm stuck. I have two scaffolds, artist and song.
In songs/new.html.erb, I have these lines:
...
<%= f.label :name %><br />
<%= f.text_field :name %>
...
<%= f.label :Artist %>
<%= collection_select(:song, :Artist, #artists, :id, :sort_name) %>
...
In the form for creating a new song, I want a <select> list with all artists. Using the code above works fine. The form is created as I want it, and the artists are listed. However, when submitting the new song, I get this error:
Artist(#69916999335860) expected, got String(#69917057438720)
The generated HTML code for the select looks like this:
<select id="song_Artist" name="song[Artist]">
<option value="1">Vreeswijk, Cornelis</option>
<option value="2">De lyckliga kompisarna</option>
<option value="3">Wiehe, Mikael</option>
<option value="4">Demian, Lars</option>
<option value="5">Sundström, Stefan</option>
</select>
I guess the second last parameter for collection_select() is wrong, but what should it be?
I think this should be:
<%= collection_select(:song, :artist_id, #artists, :id, :sort_name) %>
The second parameter is the method to be assigned in the model being created/updated. So in your controller the value would be retrieved from the params hash with params[:song][:artist_id]
A detailed explanation can be found in the Rails API docs under "collection_select"