Rails Engine Questions

Hi all,

First, I hope everyone has a happy new year and a fun productive programming year in 2011 with Rails!

I've been working extensively with the Rails::Engine in 3.0.3 and decided that it was better to move to edge rails since some of the design, especially routing, was becoming depracated in 3.1.

My 3.0.3 rails engine works fine. My 3.1 engine is still much of a mystery until I can figure out how some of the endpoints are working. I've been looking over:

.. and from what I can gather, if you do not set up an endpoint to a rack application, the default routing under your_engine/config/routes.rb becomes an endpoint.

Normally in my engine routes I would define the routes by using:

Rails::Application.routes draw do # routes.. end

but in the documentation it states that I should be able to do:

MyEngine::Engine.routes.draw do # routes.. end

When I require the gem to my new engine using the older way, all of the engine routes show up fine. When I require the gem to my new engine using the newer way, zero routes show up. I've tried to read up more on the way this endpoint is working, but I'm not getting as far as I'd like.

I could use some help and perhaps even some quality resources/links to the edge rails way of creating engines, especially where routing is concerned.

Thanks.

It appears I can still use:

Rails.application.routes.draw do # routes end

.. but I'd like to ensure that I do things more akin to the way the engine code is being handled.

Okay I'm making some headway but again, it's not comfortable yet.

I'm able to do the following:

(from the engine app) my_engine/config/routes.rb

MyEngine::Engine.routes.draw do   resources :test_index, :controller => 'my_engine/test_index' end

(from the main app that uses the engine) /config/routes.rb

Testapp::Application.routes.draw do   mount MyEngine::Engine => '/my_engine', :as => 'my_engine' end

And this allows me to use the new engine routes. The only drawback I've seen so far is that you have to change the way the paths load in the views. For instance, if I have an index view that has a standard show/edit/destroy/new..

# my_engine/app/views/my_engine/test_index/index.html.erb <% @test_indexes.each do |test_index| %> <%= link_to 'Show', my_engine.test_index_path(test_index) %> <%= link_to 'Edit', my_engine.edit_test_index_path(test_index) %> ETC.. <% end %>

If I don't include the mountable call to my_engine and the path as a method, I'll receive an undefined method hash_for_ error. So, the above seems to work fine.

However, if I run rake routes in the new testapp it doesn't show all of the routes available. It just shows:

my_engine /my_engine (:to=>MyEngine::Engine)

.. so not sure if I like that or how to be able to see all of the routing paths available from the engine in the new test application. But, making some progress anyways.

Okay, it took me a few hours but I completed my first engine for rails 3.1 edge. I found several very handy articles and start points in case anyone else wants to learn more about rails engines. Here are the links and resources I found:

http://piotrsarnacki.com/2010/09/14/mountable-engines/ http://edgeapi.rubyonrails.org/classes/Rails/Engine.html http://www.builtfromsource.com/2010/12/13/mountable-engines-in-rails-3-1-beta-getting-started/ https://github.com/krschacht/rails_3_engine_demo

If anyone has questions, I'd be more than happy to walk you through the initial stress and labors. But, once you get it going, it feels really good.

Thanks.

I'd love to see/hear the approach to design and build an engine. How can I use an engine to share code among like projects? Can I decompose a larger project into engines? What are the considerations and pitfalls? Mantras for design?

Hi Martin, go visit this new link I created which will provide a link to an example rails engine I created. It should help answer all of your questions.