我的 Controller 中有一个散列, View 从中获取数据以显示。在我看过的教程中,我学会了如何显示散列中的每个键、值对...但是如何只显示我想要的键、值对?
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 %>
最佳答案
想要显示标题,就求标题吧!无需循环,您可以像这样直接访问哈希的所有值:
<strong><%= @app['title'] %></strong>
关于ruby-on-rails - 如何在 View 中显示散列中的特定键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15412242/