Poluxland
Poluxland

Reputation: 53

Show the last object with some condition Rails

In Rails I want to put a link in the view to the last object with some condition, I know how to found the object, like this code

Equipment.where(name: "Bulldozer").last

But I don´t know the right syntax to put in the href to go to the show of the last Equipment with name Bulldozer

<td> <a href="<% link_to Equipment... %>"> text </a> </td>

Thanks in advance!!!!

Upvotes: 0

Views: 52

Answers (1)

mr_sudaca
mr_sudaca

Reputation: 1176

you're almost there, you need to assign the object to an instance variable, it'll be available within the view. So, in the controller action:

@equipment = Equipment.where(name: "Bulldozer").last

then, in your view:

<td> 
  <%= link_to "text", equipment_path(@equipment) %>
</td>

link_to helper will generate the <a> tag for you

Upvotes: 3

Related Questions