0

I'm using simple_form to submit a form with multiple fields and some dropdown values. When the form is rendered with errors, all text fields are remembered, but some of the dropdown values are resetted. All values are permitted in strong params, and pass validate_presence_of validation.

The collections are created in my model using class methods. As follows:

def self.options
 ['One','Two','Three']
end

And loaded in my form using:

<%= f.input :dropdown, collection: MyModel.options, include_blank: false %>

What should I do correctly render the form object when it is returned with errors?

Matthias
  • 1,884
  • 2
  • 18
  • 35

1 Answers1

1
f.select :dropdown, MyModel::myoptions, {include_blank: false}

And the method should be like this

   def self.myoptions
    [["One","One"],["Two","Two"],["Three","Three"]]
   end

Please try and let me know.

uday
  • 8,544
  • 4
  • 30
  • 54
  • Thanks for your answer. What is the difference between MyModel.myoptions & MyModel::myoptions? Why exactly do you suggest putting the options into an subarray? – Matthias May 21 '16 at 10:59
  • Since you are using 'self' while defining the method. To access class methods you need to use :: and to access instance methods you use . in Ruby – uday May 21 '16 at 11:03
  • Thats how `f.select` works. one value would be for display and another one would be for the `value` attribute for the ` – uday May 21 '16 at 13:53
  • This works for me: `f.select :dropdown, collection: MyModel::myoptions, include_blank: false`. Thanks for the help – Matthias May 21 '16 at 22:25
  • Glad it worked! Don't forget to accept my solution as answer ;) – uday May 22 '16 at 02:39
  • Will do. Question: let's say you have ['one', 'one'] and wish the value to change per locale (but the key to stay the same). Is there a simple way of doing this? – Matthias May 22 '16 at 15:03
  • Good question, I haven't done in model it but may be you can have entries in locale files, and use I18n to do translations on view for you. http://stackoverflow.com/questions/7924326/rails-3-translations-within-models-in-production http://guides.rubyonrails.org/i18n.html – uday May 22 '16 at 15:13