Rails newbie here, writing a sample app that has the following three models and relationships:
Sales person:
class Salesperson < ActiveRecord::Base
has_many :clients
Client:
class Client < ActiveRecord::Base
has_many :orders
belongs_to: salesperson
Orders:
class Order < ActiveRecord::Base
belongs_to :client
On the page clients/show.html.erb I have a partial that renders the following:
<table>
<tr>
<th>Name</th>
<th>Total Orders</th>
<th>Email</th>
<th></th>
</tr>
<% @salesperson.clients.each do |client| %>
<tr>
<td><%= client.full_name %></td>
<td><%= client.orders.count %></td>
<td><%= client.email %></td>
<td><%= link_to 'View Client', client_path %></td>
</tr>
<% end %>
</table>
Resources are all nested, and the page seems to work except for one thing:
Everything is dynamic but the client path: the client name, orders, emails are all displayed for the salesperson, but the view client link seems to always point at a clients/:id link where :id is the salesperson id but not the client ID.
E.g. because the salesperson :id is 1, all the client paths will point to clients/1 down the whole table.
How do I make the client link route correct and dynamic like the rest of the table?
Try with
or simpler
client_pathneed a parameter that is aClientor an id.