J
J
Japanmama2016-02-14 18:25:36
Ruby on Rails
Japanmama, 2016-02-14 18:25:36

Rails 4. How to make a loadable value from the database?

Good afternoon,
In one of the forms I have a select field that contains the following construction:
<%= f.select :dic_country_id, options_from_collection_for_select(@countries, "id", "country_name") %>
You can select a country in the field, let's say Russia , then saved successfully. The next time the form is accessed, the country selection list reappears in alphabetical order, and I need the value from the database to be loaded into the select field (i.e. the starting point was Russia, not Abkhazia, because it is the first in alphabetical order) .
Thank you in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Demidenko, 2016-02-14
@JapanMama

If you want from the database, then try to set the value yourself as the fourth parameter for options_from_collection_for_select
Something like this:

<%= f.select :dic_country_id, options_from_collection_for_select(@countries, "id", "country_name",  значение_с_объекта) %>

J
Japanmama, 2016-02-14
@JapanMama

Thanks for the solution, everything worked.
It is important to remember that the fourth parameter must be of type integer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question