27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

514 CHAPTER 17 Engines<br />

This task actually copies across the engines migration into the application’s db/<br />

migrate directory, with the first migration having a timestamp of the current second,<br />

the next migration having it one second in the future, and so on. This is so the<br />

engine’s migrations are inserted after the current application’s migrations and maintain<br />

the same order that they have in the engine. If you update the engine later on,<br />

you’ll have to run this task again. To install these migrations, you’ll run this:<br />

rake db:migrate<br />

After this, you’ll need to mount the engine in your application’s config/routes.rb,<br />

which you can do with this:<br />

mount Forem::Engine, :at => "/forem"<br />

You saw this line in your spec/dummy/config/routes.rb file earlier. Mounting your<br />

engine will make its routes available to the specified path. If you launch rails server<br />

now and go to http://localhost:3000, you’ll be shown this error:<br />

Please define Forem::Engine.user_class in config/initializers/forem.rb<br />

You should follow the advice for this error and define Forem::Engine.user_class<br />

within the application’s config/initializers/forem.rb, as you need to tell the engine<br />

what the user class is:<br />

Forem::Engine.user_class = User<br />

Because you’ve created a new initializer, you’ll need to restart the rails server that is<br />

currently running so that Rails re-evaluates the files in the config/intializers directory.<br />

When you do this and refresh the page, you’ll see this error:<br />

undefined local variable or method `admin_root_path' ...<br />

This is happening because your engine is using the application’s layout and is trying<br />

to reference admin_root_path method from inside the engine, rather than the one<br />

that’s defined in the application. To fix this, you’ll need to first call main_app for these<br />

routing helpers and then call the helpers on that. You need to change the root_path,<br />

admin_root_path, destroy_user_session_path, new_user_registration_path, and<br />

new_user_session_path helpers in the app/views/layouts/application.html.erb file to<br />

all have the main_app. prefix on them.<br />

These are all the changes you need to make in order to integrate your engine with<br />

your application. Click around a bit and try it out!<br />

17.10 Summary<br />

In this very long chapter we’ve covered quite the gamut! We’ve gone through the theory<br />

first of all, the history of engines, and why they’re useful. After that, you saw the<br />

layout of one and how the routing for an engine works.<br />

You then spent the rest of the chapter creating your own engine called forem,<br />

which provides the basic functionality of a forum: topics and posts.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!