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.
Related
On a model, I have attr_accessor: :email_settings.
In a view, I have:
<%= form_for some_model do |f| %>
<%= f.fields_for :email_settings do |email_settings| %>
<%= email_settings.label :general, _("General updates") %>
<%= email_settings.check_box :general %>
General site updates
<% end %>
<% end %>
But in the HTML, this does not create one input for some_model[email_settings][general], it creates two. One hidden and one a checkbox:
<label for="user_email_settings_general">General updates</label>
<input name="user[email_settings][general]" type="hidden" value="0">
<input id="user_email_settings_general" name="user[email_settings][general]" type="checkbox" value="1">
General site updates
What's going on here? Why is there a hidden input and a checkbox for the same value, when I only want a checkbox?
Also in the controller action that the form is submitted to, I do this:
def update
puts "email_params: #{email_params}"
end
def email_params
params.require(:user).permit(:email_settings)
end
Which outputs:
Unpermitted parameters: email_settings
email_params: {}
Not sure how email_settings is being interpreted as "unpermitted" when I'm explicitly permitting it.
From the browser should always send a response.
If you do not select the box, will send the hidden field to understand that something has been sent.
In this way the rails will always receive an indication that the box was selected or not.
similar answer is here: Why does the check_box form helper generate two checkboxes, one hidden?
I have a rails form to update company information it calls on the update action, I have a field that I want to get the value of as a parameter on submit, the field is not included as a attribute of company, so the value is currently not coming in as a parameter. Is it possible to get the value of this field? I am using the select2 gem in the field that I need to get the value from. The field is below.
<div id="allStations">
<select multiple id="list-markets" style="width:350px">
<% Market.all.each do |market| %>
<option value="<%= market.id%>"><%= market.name%></option>
<% end %>
</select>
</div>
If you want to submit a value from a field that is not an attribute on the object, then you can use a FormTagHelper. These are the helpers that end in _tag. You might want to try something like:
<div id="allStations">
<%= select_tag "list-markets", options_from_collection_for_select(Store.all, :id, :name), style: "width: 350px", multiple: true %>
</div>
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 a form that will pass 2 parameters to my controller. My question is every time I make a choice in the select_tag form, I want my option to stay after I hit the submit tag. That way the user knows what he or she just selected. I could used :selected=>"true", but thats only for the default value and not for the value submitted.
<form name="filter" action="" style="display:inline" >
<label for="filter">Filter by Name or Description: </label>
<%= text_field_tag "query", params['query'] %>
<label for="status">Filter by Status:</label>
<%= select_tag(:sortstatus,
'<option value="empty">Show All</option>,
<option value="0">Applying</option>,
<option value="3">Suspended</option>,
<option value="4">Pending</option>') %>
<%= submit_tag 'Search' %>
</form>
And here is the controller that will change the value of empty to work with my table
def sort_status
if params[:sortstatus] == "empty"
#statusorder = ""
else #statusorder = params[:sortstatus]
end
end
Haven't been able to find any solution so far in Google.
Take a look at using options_for_select to generate your options tags. It allows you to specify which entry you would like to be selected. e.g.
<%= select_tag(:sortstatus, options_for_select([['Show All', 'empty'],
['Applying', '0'],
['Suspended', '3'],
['Pending', '4']], params[:sortstatus]) %>
This will set the selected item to the current value of params[:sortstatus]
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"