Investing the same resource in several places on routes

Therefore, I, like many others, present the concept of β€œlikes” in my application. There are many different types of resources that you might like (messages, answers, lists, actions, etc.), and I'm looking for the best way to structure my routing file.

This may be due to the creation of a route.rb file that looks something like this:

resources lists do resources posts do resources replies do resources likes end resources likes end resources likes end 

and LikesController, which does not know what load_and_authorize means:

 class LikesController < ActionController:Base load_and_authorize :list # only works if list_id exists in this request load_and_authorize :post, through: :list # only works if post_id exists in this request # and so on... end 

Are there any better ways to do this? I was thinking that I have a way / like that all the messages, but that means that I can not use load_and_authorize, and also means that it is harder to do things like Likes # index for a given list, message, response and etc.

Thanks.

+4
source share
1 answer

Instead of nesting like in other resources, try the following:

 scope "/lists/:list_id(/posts/:post_id(/replies/:reply_id))" do resources :likes end 

I believe this should lead to a single set of LikesController routes that require the parameter :list_id , and which optionally accepts :post_id and :reply_id . The url is pretty ugly, but I'm just trying to match the default value for a resource route with four nested rails. I recommend cleaning it a bit.

Edit: It looks like this will require some additional logic in your LikesController - for example, before_filter, which determines what parameters you have, and load_and_authorizes only those objects. But for me, does this seem like a solvable problem?

0
source

Source: https://habr.com/ru/post/1435038/


All Articles