mahatmanich
mahatmanich

Reputation: 11013

Rails 4 collection_select with specific locale variable input parameters

I have a legacy database with a table country that has country names in different languages.

The columns are:

country_de
country_en
country_fr
country_it

My app uses i18n.locale and now I want to populate the country collection_select with the specific locale parameters.

I know I could use a simple if else or switch and set the params statically but is there a way to do this in one line?

Working example for English:

<%= f.collection_select :country_id, Country.order('country_en ASC'), :id, :country_en, {prompt: t("select_country")} %>

What I am trying at the moment and is not working is this:

<%= f.collection_select :country_id, Country.order('country_'+locale.to_s+' ASC'), :id, "country_"+locale.to_s+"".to_sym , {prompt: t("select_country")} %>

Where locale will be de, en, it, or fr of course.

The error that I have is:

no implicit conversion of Symbol into String

Thanks for the help

Upvotes: 0

Views: 329

Answers (1)

uno_ordinary
uno_ordinary

Reputation: 458

Replace:

"country_"+locale.to_s+"".to_sym

With:

"country_#{locale}".to_sym

Better use string interpolation, which implicitly calls to_s

Upvotes: 1

Related Questions