dyserone dyserone - 2 months ago 8x
Ruby Question

Rails forms, change field name dynamically with iteration variable?

I have an iteration for all my locales.

I want the :text and :title field on every iteration to change depending on the locale, for example :title_en for English, :title_ru for Russian, :title_gr for Greek and so on.

The "en", "ru", "gr" and so on are the locale in my iteration so I get them from there.

<% I18n.available_locales.map.with_index(1) do |locale, index| %>
<%= f.text_field :title_(I want to use the locale here) %>

How do I go about that? What is the best way to do it?


If your form builder support all locales, you can try this:

<% I18n.available_locales.each.with_index(1) do |locale, index| %>
   <%= f.text_field "title_#{locale}".to_sym %>