Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / creating_plugins / controllers.txt
1 == Add a controller ==
2
3 This section describes how to add a controller named 'woodpeckers' to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.
4
5 You can test your plugin's controller as you would test any other controller:
6
7 *vendor/plugins/yaffle/yaffle/woodpeckers_controller_test.rb:*
8
9 [source, ruby]
10 ----------------------------------------------
11 require File.dirname(__FILE__) + '/test_helper.rb'
12 require 'woodpeckers_controller'
13 require 'action_controller/test_process'
14
15 class WoodpeckersController; def rescue_action(e) raise e end; end
16
17 class WoodpeckersControllerTest < Test::Unit::TestCase
18 def setup
19 @controller = WoodpeckersController.new
20 @request = ActionController::TestRequest.new
21 @response = ActionController::TestResponse.new
22 end
23
24 def test_index
25 get :index
26 assert_response :success
27 end
28 end
29 ----------------------------------------------
30
31 This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
32
33 *vendor/plugins/yaffle/lib/yaffle.rb:*
34
35 [source, ruby]
36 ----------------------------------------------
37 %w{ models controllers }.each do |dir|
38 path = File.join(File.dirname(__FILE__), 'app', dir)
39 $LOAD_PATH << path
40 ActiveSupport::Dependencies.load_paths << path
41 ActiveSupport::Dependencies.load_once_paths.delete(path)
42 end
43 ----------------------------------------------
44
45
46 *vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:*
47
48 [source, ruby]
49 ----------------------------------------------
50 class WoodpeckersController < ActionController::Base
51
52 def index
53 render :text => "Squawk!"
54 end
55
56 end
57 ----------------------------------------------
58
59 Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to http://localhost:3000/woodpeckers to see your controller in action.