When I access to the url like example.com/shop?genre=13,
It should automatically show the default selection set with the value 13.
However, it shows very first line of selection.
Why?
<%= select_tag :genre, options_for_select(Genre.all.map{ |g| [g.name, g.id] }), :selected => params[:genre] %>
If you want to select an option, you need to pass the value to the
options_for_selectmethod. If you lookup the signature for the method you will find:Further reading of the docs at http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select will lead you to the example:
In your case you should be able to get it working with:
As a side note. I guess
Genreis an ActiveRecord model. In this case you can useoptions_from_collection_for_select. This method is designed to create a list of options tags from an array of ActiveRecord models. You find the docs at: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_selectYour code could look something like: