Home Find resource by slug in nested resource in rails
Reply: 1

Find resource by slug in nested resource in rails

Rabin Poudyal
Rabin Poudyal Published in 2017-12-04 12:49:44Z

I have following routes

 resources :shops do
    resources :categories

And when I visit this url:


I first want to find the shop by using slug 'rabin-shop', then I can filter the categories of products that belong to that shop. In my controller I have tried to implement

    def find_shop
       @shop = Shop.find(params[:slug])

But this is not working. I know this is not how to find in nested resource. I am using friendly_id gem . I cannot do something like current_user.shop because I want the page to be accessed even when the user is not logged in.

Snake Reply to 2017-12-04 12:51:19Z

If you are using the freindly_id gem, this is how you find a record by it's slug :

 def find_shop
   # params[:shop_id] if nested
   @shop = Shop.friendly.find(params[:id])
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328221 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO