Ask A Question

Notifications

You’re not receiving notifications from this thread.

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.

Reply

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.

Reply

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
Reply

Neat episode!

Reply

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
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,584+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.