Domanda

I have two different isolated mountable rails engines; one named Core, other as Finance;

The core engine has a comment resource and routing concern like;

Core::Engine.routes.draw do

  concern :commentable do
    resources :comments
  end

end

And the Finance engine has a invoice model;

Finance::Engine.routes.draw do

  resources :invoices, concerns: :commentable

end

Both these engines added main app's Gemfile, and routes.rb file like below; Gemfile;

gem 'core', path: "../core"
gem 'finance', path: "../finance"

routes.rb;

mount Core::Engine, at: "/"
mount Finance::Engine, at: "/"

At the finance gem; invoice show.erb has comment form like below;

<%= form_for [@invoice, @comment] %>

but it seems rails 4 can't share routing concerns between engines. I have found so many questions on stackoverflow, but still can't find a good solution.

Maybe this not avaliable in rails engines; is there any way two handle this.

Thanks.

È stato utile?

Soluzione 2

I don't think that it's possible to do that because each engine is its own container and you can't reach across between engines to do what you're attempting to do.

Instead, define a module which you can include in both contexts which define the same concern:

module CommentableConcern
  def self.included(base)
    base.instance_eval do
      concern :commentable do
        resources :comments
      end
    end
  end
end

I think this is the only way you can accomplish that.

Altri suggerimenti

Struggling a little bit with Ryan's answer (how to correctly include it where?), I came up with the following solution without explicitly using concerns but still sharing routes:

# 1. Define a new method for your concern in a module,
# maybe in `lib/commentable_concern.rb`

module CommentableConcern
  def commentable_concern
    resources :comments
  end
end

# 2. Include the module it into ActionDispatch::Routing::Mapper to make
# the methods available everywhere, maybe in an initializer

ActionDispatch::Routing::Mapper.include CommentableConcern

# 3. Call the new method where you want
# (this way `concerns: []` does not work) obviously

Finance::Engine.routes.draw do
  resources :invoices do
    commentable_concern
  end
end

This monkey patches ActionDispatch::Routing::Mapper, but as long as you only define new methods (and do not touch existing ones) it should be safe.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top