Reputation: 5132
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 %>
Upvotes: 1
Views: 2347
Reputation: 10395
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>
Upvotes: 4
Reputation: 29599
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
Upvotes: 1