Reputation: 3432
I have this loop in my page that displays all the cities in my model. Lets say New York City is one of these cities and I just want to access New York City. How do I access a particular city instance from my model in Rails?
<% @cities.each do |city| %>
<tr>
<td><%= city.name %></td>
<td><%= city.country %></td>
<td><%= link_to 'Show', city %></td>
<td><%= link_to 'Edit', edit_city_path(city) %></td>
<td><%= link_to 'Destroy', city, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Upvotes: 0
Views: 46
Reputation: 1069
Use an attribute of that object to access the object in the view:
For example, if the name
attribute of your city was "New York City", then you would say:
City.find_by_name("New York City")
Of course, it's not super railsy to do this in the view, so just add this to to relevant controller action:
@newyork = City.find_by_name("New York City")
and use @newyork
in your view.
Upvotes: 1