How can I make routes from a Rails 3 engine available to the host application?

谁说胖子不能爱 提交于 2020-01-01 01:53:08

问题


I have a Rails 3 application with several engines containing additional functionality. Each engine is a separate service that customers can purchase access to.

I am, however, having a problem with routes from the engines that aren't readily available to the controllers and views.

controller:

class ClassroomsController < ApplicationController
  ..
  respond_to :html

  def index
    respond_with(@classrooms = @company.classrooms.all)
  end

  def new
     respond_with(@classroom = @company.classrooms.build)
  end

  ..
end

app/views/classrooms/new.html.haml:

= form_for @classroom do |f|
  ..
  f.submit

config/routes.rb in engine:

MyEngineName::Engine.routes.draw do
  resources :classrooms
end

config/routes.rb in app:

Seabed::Application.routes.draw do
  mount MyEngineName::Engine => '/engine'
  ...
end

lib/my_engine_name.rb in engine:

module MyEngineName
  class Engine < ::Rails::Engine
  end
end

attempting to go to /classrooms/new results in

NoMethodError in Classrooms#new

Showing app/views/classrooms/_form.html.haml where line #1 raised:
  undefined method `hash_for_classrooms_path' for #<Module:0x00000104cff0f8>

and attempting to call classrooms_path from any other view results in the same error. I can, however, call MyEngineName::Engine.routes.url_helpers.classrooms_path and get it working. I'm thinking I might have defined the routes wrong, but can't find another way that works.

Tried running the app with both Passenger (standalone and Apache module) and WEBrick (rails server). Using latest Rails from Git (7c920631ec3b314cfaa3a60d265de40cba3e8135).


回答1:


Change config.routes in your engine to:

Rails.application.routes.draw do  # NOT MyEngineName::Engine.routes.draw
  resources :classrooms
end

The way you have it, the routes are only available in the MyEngineName::Engine namespace and not in the rest of the host rails application.

There used to be a blog post with more info, but unfortunately it is no longer available:

  • http://blog.loopedstrange.com/modest-rubyist-archive/rails-3-plugins-part2-writing-an-engine



回答2:


I had the same problem, and found this in the documentation:

Since you can now mount an engine inside application’s routes, you do not have direct access to Engine‘s url_helpers inside Application. When you mount an engine in an application’s routes, a special helper is created to allow you to do that. Consider such a scenario:

# config/routes.rb
MyApplication::Application.routes.draw do
  mount MyEngine::Engine => "/my_engine", :as => "my_engine"
  get "/foo" => "foo#index"
end

Now, you can use the my_engine helper inside your application:

class FooController < ApplicationController
  def index
    my_engine.root_url #=> /my_engine/
  end
end



回答3:


For me also help to add

require 'engine' if defined?(Rails)

to my main gem file (lib/.rb).

Good example - https://github.com/mankind/Rails-3-engine-example/blob/master/lib/dummy.rb



来源:https://stackoverflow.com/questions/4361050/how-can-i-make-routes-from-a-rails-3-engine-available-to-the-host-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!