我从外部网站获得了以下Feed,我已通过httparty将其引入我的rails3.2应用。
似乎我有一个重新开始的时刻,无法弄清楚如何格式化/设计风格。我需要使用nokogiri还是更简单?
["radcheck", [{"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>22, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"simon", "value"=>"pass"}, {"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>23, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"silver", "value"=>"rain"}.....
答案 0 :(得分:1)
Hum我认为你所要做的就是遍历第二个元素,这是一个哈希数组:
在控制器中:
@feed = ... # Retrieve your feed with httparty or other
在视图中:
<p>
First element: <%= @feed[0] #="radcheck", don't know the meaning of this %>
</p>
<p>
Attributes:
<ul>
<% @feed[1].each do |item| %>
<li>
Name: <%= item.attribute_name %><br />
Batch User ID: <%= item.batch_user_id %><br />
ID: <%= item.id %><br />
OP: <%= item.op %><br />
... and so on ...
</li>
<% end %>
</ul>
</p>
答案 1 :(得分:1)
给出您的样本数据:
feed = ["radcheck", [
{"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>22, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"simon", "value"=>"pass"},
{"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>23, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"silver", "value"=>"rain"}
]]
if feed.is_a? Array && feed.size >= 2
radcheck = feed.first # do you need this first element?
data = feed[1]
# now you have an array in data, and can loop over each hash
Array(data).each do |elem|
puts elem["id"] # and so on
end
end
这适用于我的控制台,因此您应该能够根据您的模板机制进行调整。