Rails 3 Choose the default value for the menu when editing an entry

I am using the following code for the select menu in my rails application

Controller:

@discount_options = {'% Off' => 'percent', '$ Off' => 'dollar', '$ For' => 'flat', 'Free with Purchase Of' => 'bonus', 'Buy One Get One Free' => 'bogo' } 

View (inside form_for)

 <%= f.select :discount, options_for_select(@frugle_discount_options) %> 

This works great when creating a new record. If I go back to edit this entry, it always ends with the default "% Off"

I find it surprisingly difficult to find good documentation online about menu selection .. can someone help me?

Thanks!

+4
source share
3 answers

You can pass the second parameter to

 options_for_select_method(@discount_options, 'bonus') 

You can install it using: discount option.

+3
source

You must link to f.object with: selected

  <%= f.select :discount, options_for_select(@frugle_discount_options), :selected => f.object.discount %> 
+2
source

Here is a great blog that I came across that discusses the use of various pick assistants in rails. Worth to read.

http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails

+1
source

Source: https://habr.com/ru/post/1347042/


All Articles