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
).
Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
root 'topics#index' is a Rails route that says the default address for your site is topics#index . topics#index is the topics list page (the topics controller with the index action). Rails routes control how URLs (web addresses) get matched with code on the server.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With