Routing Concerns and Scopes in Rails Discussion
Hey Chris, thanks for making a video on this and so quickly. I had been wanting to use routing concerns for a while but finding a good way to access the resource had me stumped. This does the trick.
Alternative approach to using @scope:
resources :todos, likeable_type: "Todo" do
concerns :likeable
end
And you can use params[:likeable_type]
in your Likes controller.
I find it a bit more explicit and safer as I know exactly what strings I’ll be constantizing.
Interesting. I don't think I've seen this documented anywhere. It seems like it would filter out the only:
and other options and the leftover ones are set as defaults?
I know you can do this:
resources :todos do
scope defaults: { record_type: "Todo" } do
concerns :likeable
end
end
The concern definition block actually takes a hash of options, so another approach could look like:
concern :likeable do |options = {}|
resource :like, only: :update, defaults: **options
end
resources :comments do
concerns :likeable, resource: :comments
end