27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Writing your first engine feature<br />

Listing 17.2 spec/support/load_routes.rb<br />

RSpec.configure do |c|<br />

c.include Forem::Engine.routes.url_helpers,<br />

:example_group => {<br />

:file_path => /\bspec\/integration\//<br />

}<br />

end<br />

This will load the URL helpers, such as topics_path, that you need for your test.<br />

One interesting thing to note here is that your topics_path method doesn’t generate<br />

the normal /topics URL as would be expected. Instead, it generates the correct<br />

/forem/topics path. This is because your engine is mounted in spec/dummy/config/<br />

routes.rb under the forem path. When you visit topics_path, you’re actually going<br />

to visit the correct path of this route, like you would in a real application.<br />

The next time you run your spec, you’ll see this error:<br />

ActionController::RoutingError:<br />

uninitialized constant Forem::TopicsController<br />

Now your topics_path helper is working and generating a route to the index action<br />

inside Forem::TopicsController, which you attempt to visit by using the visit<br />

method. This controller doesn’t exist right now, and therefore you get this error. So<br />

let’s generate this controller to proceed.<br />

17.5.3 The topics controller<br />

You’ve come to the stage where you need the first controller for your application,<br />

Forem::TopicsController. To generate this controller, you can run this command:<br />

rails g controller forem/topics<br />

You have to namespace your controller by putting forem before it so that Rails creates<br />

it correctly. This command will generate the normal controller stuff for your engine,<br />

such as the controller itself, a helper, and the app/views/forem/topics directory.<br />

What’s your spec tell you next? Let’s find out with bin/rspec spec/integration/<br />

topics_spec.rb:<br />

AbstractController::ActionNotFound:<br />

The action 'index' could not be found for Forem::TopicsController<br />

You now need to create the index action in Forem::TopicsController.<br />

17.5.4 The index action<br />

Let’s open app/controllers/forem/topics_controller.rb now. Inside this controller,<br />

you’ll see this:<br />

module Forem<br />

class TopicsController < ApplicationController<br />

end<br />

end<br />

485

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

Saved successfully!

Ooh no, something went wrong!