Search code examples
rubyhtml-selectruby-on-rails-5dropdown

Select tag : How to use params to set the selected value?


I have a select tag with 2 options :

 select_tag :type,  options_for_select("<option value = 'produit1'>Produit1</option><option value = 'Produit2'>Produit2</option>", params[:product]), id: "different_option_value_html", onchange: "this.form.submit();", include_blank: "Which products ?"

When one of this options are selected, the form is automatically submit. I would like to keep the params in first position. I mean visible without clicking on the select, at the place of "which products".


Solution

  • I finally change the syntax and it works fine using :options_for_select(:collection, :selected)

    So there is my code now :

    = select_tag :product,  options_for_select(["Product1", "Product2"], params[:product]), id: "different_option_value_html", onchange: "this.form.submit();", include_blank: "Which product ?"