New Discussion

Notifications

You’re not receiving notifications from this thread.

Routing Concerns and Scopes in Rails Discussion

5
General

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

Neat episode!

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

Below is a discussion of both concepts, their uses, and differences, based on standard Rails practices and insights from available web resources.

Join the discussion
Create an account Log in

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

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

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