[英]How do I display specific key value pairs from a hash in the view?
I have a hash in my controller that a view takes data from to display. 我的控制器中有一个哈希,一个视图将数据从中显示出来。 In the tutorials I've seen, I've learned how to display each of the key, value pairs from a hash...but how do I display only the key,value pairs I want? 在我看过的教程中,我学习了如何显示来自哈希的每个键,值对...但是如何只显示想要的键,值对?
creating the hash in the controller
@app = {'title' => title, 'description' => description,
'active' => active, 'featured'=> featured,
'partner'=>partner
}
view: this displays each of the key,value pairs
<% @app.each do |key, value| %>
<li><%= "#{key}: #{value}" %>
<% end %>
tried this in the view just to display title, but isn't working
<% @app.select do |ind_app| %>
<strong><%= ind_app["title"] %>
<% end %>
If you want to display the title, just ask for the title! 如果要显示标题,只需询问标题! No need to loop, you can directly access all values of a hash like this : 无需循环,您可以像这样直接访问哈希的所有值:
<strong><%= @app['title'] %></strong>
you can try to get the pairs you want first. 您可以尝试先获得想要的对。 Try the following 尝试以下
<% @app.slice('title', 'active').each do |key, value| %>
<li><%= "#{key}: #{value}" %>
<% end %>
This will only show the title and active part of the hash 这只会显示哈希的标题和有效部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.