/items should list all items
/user/items should list only items for the current user
I have defined a relationship between users and items, so current_user.items works fine.
My question is:
How does the items#index action know whether it has been called directly or via a user resource in order to know what to populate @items with?
# routes.rb
resources :items
resource :user do
resources :items
end
# items_controller.rb
class ItemsController < ApplicationController
def index
@items = Item.all
end
end
# users_controller.rb
class UsersController < ApplicationController
def show
@user = current_user
end
end
It looks hacky, but it was my first idea :)
resource :user do
resources :items, :i_am_nested_resource => true
end
class ItemsController < ApplicationController
def index
if params[:i_am_nested_resource]
@items = current_user.items
else
@items = Item.all
end
end
end
Or you can go brute way: parse your request url :).
I solved this by separating the code into two controllers (one namespaced). I think this is cleaner than adding conditional logic to a single controller.
# routes.rb
resources :items
namespace 'user' do
resources :items
end
# items_controller.rb
class ItemsController < ApplicationController
def index
@items = Item.all
end
end
# user/items_controller.rb
class User::ItemsController < ApplicationController
def index
@items = current_user.items
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With