如何在Rails的视图中检索哈希值 [英] How to retrieve the hash values in the views in rails

查看:99
本文介绍了如何在Rails的视图中检索哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有一个动作:

I have an action in the controller:

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

在视图中:

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

我正在尝试检索@user_detail的内容:实际上,哈希包含{:empid=>"11111", :prjtname=>"aaaaa", :prjtrole=>"Developer"}

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

如何显示用户详细信息的empid和其他值?

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 %>

这篇关于如何在Rails的视图中检索哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆