How to get array values ​​in Ruby on Rails

I am working on the concept of Localization in Rails and should get some localization values ​​in HTML pages. So, I created an array in the controller, for example, below.

#array use to store all localization values @alertMessages = [] #array values... {:index=>"wakeUp", :value=>"Wake Up"} {:index=>"tokenExpired", :value=>"Token Expired"} {:index=>"timeZone", :value=>"Time Zone"} {:index=>"updating", :value=>"Updating"} {:index=>"loadMore", :value=>"Load More"} #....more 

In HTML pages I want to get localization values, like below or some other type,

 <%= @alertMessages['wakeUp'] %> 

therefore, it displays the value " Awakening ",

But it doesn’t work .. Maybe anyone ...

+4
source share
4 answers

It is easier to use a hash for this ( http://api.rubyonrails.org/classes/Hash.html ), which looks like an array with named indexes (or keys).

Do this:

 @alertMessages = { :wakeUp => "Wake Up", :tokenExpired => "Token Expired", . . . } 

You can also expand your hash as follows:

 @alertMessages[:loadMore] = "Load More" 

Access to it using:

 @alertMessages[:loadMore] 

You should also check i18n to make internationalization more reliable and flexible: http://guides.rubyonrails.org/i18n.html

+4
source
 # Hash to store values @alertMessages = {} #hashvalues... alertMessages[:wakeUp] = "Wake Up" alertMessages[:tokenExpired] = "Token Expired" alertMessages[:timeZone] = "Time Zone" alertMessages[:updating] = "Updating" alertMessages[:loadMore] = "Load More" #....more In HTML pages i want to get localization values like below or some other type, <%= @alertMessages[:wakeUp] %> so, it will display value is 'Wake Up', 

And try to always use characters, because the search will be fast

+2
source

The array is not suitable here, but if you still want to use it, go to the following path:

 array.find{|el| el[:index] == "wakeUp"}[:value] 

You must ignore this.

+1
source

try the following:

 <% @alertMessages.each_with_index do |alertMessage, index| alertMessage[:value] if index == "wakeUp" end %> 

Thanks.

0
source

All Articles