Trying to do something really simple--could someone please provide the correct incantation?
Basically we have Biscuit
optionally nested in User
so we'd like routes like:
/biscuits
/biscuits/1
/users/2/biscuits
/users/2/biscuits/3
etc.
We have views like biscuits/index
which calls a partial biscuits/_index
to render the list. I'd like to call this same partial from the user's profile view, users/edit
, but I'm unclear on which resource_url helpers to use:
resources :users do
resources :biscuits
end
class BiscuitsController < InheritedResources::Base
belongs_to :user, optional: true
end
users/edit.html.haml:
= render 'biscuits/index', biscuits: @user.biscuits.all
biscuits/_index.html.haml:
- biscuits.each do |biscuit|
%tr
%td= biscuit.title
%td= link_to image_tag(biscuit.file_url(:thumb,:large)), resource_url(biscuit)
%td
= link_to 'Show', resource_url(biscuit)
|
%td
= link_to 'Edit', edit_resource_url(biscuit)
This partial works fine when called from the BiscuitsController
at /users/1/biscuits
, but it bombs with NoMethodError in Users#edit undefined method 'user_url' for #<UsersController>
when called from the UsersController
at /users/1/edit
-- seems the resource_url
refers to the user here not the biscuits collection.
How could I force the resource/collection to be any collection of resources, regardless of the current controller?
What's the better way to do this?
Also, say we override UsersController#collection
and #resource
, are these methods on the UsersController
called if the route invokes the BiscuitsController
via /users/1/biscuits
? Or is only one Controller
per request ever instantiated by Rails?