my code in index.html.erb file is posted below. When I tested in a regular ruby file, I did not see any quotes/brackets in output. However, when I use same code in erb file I see quotes and square brackets displayed around each value when viewed in browser. Is there any way to get around this?

1 Answer
1

.values_at always returns an array. It will optionally accept multiple arguments and return the corresponding values from the hash. Since you're only giving a single argument, you get an array with one member.

You just want a standard lookup, either using bracket notation (current_rank[:title], etc) or fetch (current_rank.fetch(:title)). Fetch has the added option of defining a default value to prevent errors when the provided key is not present in the hash: current_rank.fetch(:key) { 'default value' }.

ERB is generally not the appropriate place to define data or behavior. Assuming you're using standalone erb templates (not backed by Rails or Sinatra), I would suggest a better option would be to define your templates separate from your ruby code, either in individual files or as strings in a standard ruby file. You can take a look at the documentation for some examples.

I am using Middleman as framework. This is all very new to me, but I will certainly read over erb documentation and learn more about defining templates separate from ruby code. Big thanks for your help, Zach!!
–
lavapjFeb 17 '13 at 4:42