如何通过列数组获取对象的值?

时间:2019-08-24 03:40:13

标签: ruby-on-rails ruby

我是RoR的新手。我有这样的列和用户数组:

@user_form_column_list = 
[#<MUserForm:0x0000560565e976c0
  user_form_id: 1401,
  site_id: 1071,
  filed_name: "Kana",
  db_column_name: "last_name",
  input_type: nil,
  input_rqrd: 1,
  order_num: 1,
  status_flg: 1>,
#<MUserForm:0x0000560565e976c0
  user_form_id: 1402,
  site_id: 1071,
  filed_name: "Kanaaa",
  db_column_name: "first_name",
  input_type: nil,
  input_rqrd: 1,
  order_num: 1,
  status_flg: 1>,...]
@users = 
[#<TUser:0x00007f107d74a6f0
  user_id: 11034,
  site_id: 1071,
  user_email: "pham.yen.vy+user2@gmail.com",
  last_name: nil,
  first_name: nil>,
#<TUser:0x00007f107d74a6f0
  user_id: 11035,
  site_id: 1071,
  user_email: "pham.yen.vy+user1@gmail.com",
  last_name: nil,
  first_name: nil>,...]

在视图中: index.html.erb:

<table border="0" cellpadding="0" cellspacing="0" class="table_top" width="100%">
  <tr>
    <th width="60">&nbsp;</th>
    <% @user_form_column_list.each do |column| %>
      <th><%= column.filed_name %></th>
    <% end %>
  </tr>
  <%= render "t_user" %>
</table>

_t_user.html.erb:

<% @users.each.with_index do |user. index| %>
<tr>
<td>
  <div align="center">
    <%= link_to "link" do %>
      <input type="button" value="編集">
    <% end %>
  </div>
</td>
<td nowrap="nowrap"><%=  %></td>
</tr>
<% end %>

所以,我的问题:如何在文件_t_user.html.erb中显示正确的值以及相应的字段?

2 个答案:

答案 0 :(得分:1)

我不确定我是否理解您的问题,从示例输入/输出中,您可以执行以下操作:

data = new Dictionary<string, string> {
      ["status"] = "@username + status,
      ["in_reply_to_status_id"] = "1167588690929115136"
};

答案 1 :(得分:0)

答案非常简单,对于每个用户,您都必须遍历各列才能显示。

<% @user.each do |user| %>
  <% @user_form_column_list.each do |column| %>
    <%= user[column.db_column_name] # assuming the db_column_name holds the user attribute %>
  <% end %>
<% end %>

我省略了任何HTML来简化答案。