Using a hash in view in rails -
hi ,
i using constant hash in model user as
myuser = { :firstname => "first name", :lastname => "last name", :designation => "my designation" }
now in views , have loop have field names (firstname,lastname,designation)
so try send field name key in
<% @userfields.sort.each |userfield| %> <tr> <td> <% @userkey=userfield%> <%= @userkey%> # gives exact field names <%= user::myuser[:@userkey]%> # doesnt gives <td></tr> <%end%>
how rectify ?? give suggestions
when use variable index hash, should not specify colon, correct syntax be:
user::myuser[@userkey]
or
user::myuser[@userkey.to_sym]
depending on value in @userkey.
however, loop myuser constant directly this:
<% user::myuser.keys.each |key| %> <tr> <td>key: <%= key %></td> <td>value: <%= user::myuser[key] %></td> </tr> <% end %>
Comments
Post a Comment