Reputation: 730
I'm trying to simply pass an object (dgroup) to another controller's (tplans) index action. I'm performing a lookup based on a foreign key, so I am passing it an object id that it must filter on, but it doesn't seem to be working. This is my code:
Within the dgroup's index.html:
<% @dgroups.each do |dgroup| %>
<tr>
<td><%= dgroup.id %></td>
<td><%= link_to 'Show TPlans', tplans_path(dgroup) %></td>
<td><%= link_to 'Edit', edit_dgroup_path(dgroup) %></td>
<td><%= link_to 'Destroy', dgroup, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Within tplan's controller:
def index
@dgid = Dgroup.find(params[:id])
@tplans = @dgid.tplans
respond_to do |format|
format.html # index.html.erb
format.json { render json: @tplans }
end
end
What am I doing wrong? Note that If I replace:
@dgid = Dgroup.find(params[:id])
with
@dgid = Dgroup.find(1)
the code works, just obviously does not find the object that I want. I was just doing this to make sure the controller code was structured correctly.
Upvotes: 1
Views: 1596
Reputation: 826
You are passing an object dgroup to your links instead of passing an id.
Try
<% @dgroups.each do |dgroup| %>
<tr>
<td><%= dgroup = dgroup.id %></td>
<td><%= link_to 'Show TPlans', tplans_path(dgroup) %></td>
<td><%= link_to 'Edit', edit_dgroup_path(dgroup) %></td>
<td><%= link_to 'Destroy', dgroup, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
or
<% @dgroups.each do |dgroup| %>
<tr>
<td><%= dgroup.id %></td>
<td><%= link_to 'Show TPlans', tplans_path(dgroup.id) %></td>
<td><%= link_to 'Edit', edit_dgroup_path(dgroup.id) %></td>
<td><%= link_to 'Destroy', dgroup.id, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Upvotes: 0
Reputation: 81
I suspect your params[:id]
is coming nil.
try
<td><%= link_to 'Show TPlans', tplans_path(:id => dgroup.id) %></td>
you will get it in your index method.
the reason is if you rake:routes you will see
tplans GET /tplans(.:format) {:action=>"index", :controller=>"tplas"}
Upvotes: 2