When i use select with multiple options, form send such get request:
/saveProduct?tags=1&tags=2
so I need to get them with params[:tags], but it takes only last string. How to get all of them?
EDIT:
<%= form_tag("/saveProduct", multipart: true, method: :get) do %>
<select multiple="multiple" class="tagsSelect" name = "tags" >
<option value = 1 >123</option>
<option value = 2 >dfsd</option>
</select>
<% end %>
Your select's name attribute should be tags[] to support multiple values.
<select multiple="multiple" class="tagsSelect" name = "tags[]" >
Now when you will do params[:tags] you will get an array of values in your action.
How to pass an array within a query string
Related
I have a select dropdown in a form but was looking to set the prompt value as "0"
<%= f.select :image_size_id, options_for_select(#image_sizes.collect{ |i| [i.dimension] }), { prompt: "Please select a Print Size" }, id: 'image_size_select' %>
This generates
<select id="image_size_select" name="cart_item[image_size_id]">
<option value="">Please select a Print Size</option>
<option value="1">10x8</option>
<option value="2">A4</option>
<option value="3">A3</option>
<option value="4">A2</option>
</select>
Using jQuery I can do this
$(document).ready(function(){
$('#image_size_select option[value=""]').val('0')
});
But I was looking to do this using the select helper. Can it be set this way?
You'll need to add it to the array of values used by options_for_select instead of using the "prompt" option, which always sets a blank value. You should also make the array of sizes have two elements in each subarray: one for the displayed text and one for the value.
<% options = [["Please select a Print Size",0]] + #image_sizes.collect{ |i| [i.dimension, i.dimension] } %>
<%= f.select :image_size_id, options_for_select(options), id: 'image_size_select' %>
You can also try this.
<%= f.select :image_size_id, options_for_select([["Please select a Print Size", 0]] + #image_sizes.pluck(:dimension, :id)), {}, id: 'image_size_select' %>
I have something like this as a select_tag :
<p><%= setting_select :ui_theme, My::Themes.themes.collect {|t| [t.name, t.id]}, :blank => :label_default, :label => :label_theme %></p>
Rite now it is collecting all the values and displaying but I want to collect only a specific value and make it default. This value has name = "Test".
Thus it should look like this and it should be default:
<option selected="selected" value="Test">Test</option>
Note : Here setting_select is a helper which is defined like this:
def setting_select(setting, choices, options={})
if blank_text = options.delete(:blank)
choices = [[blank_text.is_a?(Symbol) ? l(blank_text) : blank_text, '']] + choices
end
setting_label(setting, options).html_safe +
select_tag("settings[#{setting}]",
options_for_select(choices, Setting.send(setting).to_s),
options).html_safe
end
options_for_select allows you to pre-select an option by passing its value. Example:
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
output:
<option value="1">Lisbon</option>
<option value="2" selected="selected">Madrid</option>
More information about select tag and options_for_select.
I have an action that renders a view which contains this:
<select id ='dynamic_select'>
<option value = "<%= activity_path %>">All</option>
<option value = "<%= activity_path(:type => 'enrolled') %>">Enrolled</option>
<option value = "<%= activity_path(:type => 'redeem') %>">Redeem</option>
<option value = "<%= activity_path(:type => 'social') %>">Social</option>
</select>
What would be the correct/Rails way of rendering that select and mark as selected one of the options depending on the type parameter:
If there is no type parameter, select "All" options, if there is type=enrolled parameter select Enrolled option, and so on...
I have managed to do that client side with Javascript, but I am wondering what would be the Rails way of doing so.
Rendered HTML:
<select id="dynamic_select" name="dynamic_select">
<option value="/activity">All</option>
<option value="/activity?type=enrolled">Enrolled</option>
<option value="/activity?type=redeem">Redeem</option>
<option value="/activity?type=social">Social</option>
</select>
Something like the following. I have displayed the select_options here, but you should probably generate them in your controller and pass them through to the view.
The key is using options_for_select.
<% select_options = {"All" => activity_path} %>
<% %w{Enrolled Redeem Social}.each {|opt| select_options[opt] = activity_path(:type => opt.downcase)} %>
<% form_for(resource) do |f| %>
<%= s.select :dynamic, options_for_select(select_option, :selected => select_options[#default || "All"]) %>
<% end %>
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 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]