开发者

How to retrieve the hash values in the views in rails

开发者 https://www.devze.com 2023-01-29 09:18 出处:网络
I have an action in the controller: def user_detail @user_detail = UserDetail.find_by_id(11) end And 开发者_运维知识库in the view:

I have an action in the controller:

def user_detail
    @user_detail = UserDetail.find_by_id(11)
end

And 开发者_运维知识库in the view:

<%= @user_detail -%> // displays me like #

I am trying to retrieve the contents of @user_detail: actually the hash contains {:empid=>"11111", :prjtname=>"aaaaa", :prjtrole=>"Developer"}

How do I display the user detail's empid and other values?


Since I know what question you asked earlier, I think this is the syntax you actually want to use:

<%= @user_detail.additional_info[:empid] %>

Unless of course you renamed the name of the hash :)

Another approach, if you want all the content from the hash but the keys varies from each record, you could loop through them like this:

<% @user_detail.additional_info.each_pair do |key, value| %>
  <p>Key: <%= key %> Value: <%= value %></p>
<% end %>


To get simple debug output like the example you posted, this will handle it:

<%= @user_detail.inspect %>


try this <%= @user_detail.emplid %> <%= @user_detail.prjtname %> <%= @user_detail.prjtr %>


More of an extraction from @dln's answer

try using

<%= @user_detail[:emplid] %>
<%= @user_detail[:prjtname] %>
<%= @user_detail[:prjtr] %>

Hope this solves your prob

0

精彩评论

暂无评论...
验证码 换一张
取 消