hp001
hp001

Reputation: 79

How do you link_to a different models show page?

I have two models that I would like to work with. Clients who has_many Employees. I have it set up to list all the Employees associated to the Clients in the client#show page.

client/show.html.erb

<tbody>
<% @client.employees.each do | employee | %>
  <tr>
    <td class="list-group-item"><%= link_to render_employee_name(employee), employee %></td>
    <td><%= link_to 'Show', employee %></td>
    <td><%= link_to 'Edit', edit_employee_path( employee ) %></td>
    <td><%= link_to 'Destroy', employee, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</tbody>

But I can't seem to figure out the 'Back' link_to to be able to go back to the list page. Instead I'm stuck being diverted employees#index where it list all the name of every employee regardless of which Client it belongs to.

employees/show.html.erb

<%= link_to 'Edit', edit_employee_path(@employee) %> |
<%= link_to 'Back', employees_path %>

What do I need to change for it to go to it's parent Client show page? Thanks in advance!

Upvotes: 2

Views: 102

Answers (1)

Alexandre Voyer
Alexandre Voyer

Reputation: 819

<%= link_to 'Back', @employee.client %>

try that! :)

Upvotes: 1

Related Questions