我有以下路线
resources :shops do
resources :categories
end
当我访问这个网址时:
http://localhost:3000/shops/rabin-shop/categories
我首先想通过slug'rabin-shop'找到商店,然后我可以过滤属于该商店的产品类别。在我的控制器中,我试图实现
def find_shop
@shop = Shop.find(params[:slug])
end
但这不起作用。我知道这不是如何在嵌套资源中找到的。我正在使用friendly_id gem。我不能做像current_user.shop这样的事情,因为我希望即使用户没有登录也要访问该页面。
答案 0 :(得分:0)
如果您正在使用freindly_id gem,那么就是通过它的slug找到记录的方式:
def find_shop
# params[:shop_id] if nested
@shop = Shop.friendly.find(params[:id])
end