8

I'm trying to contribute to an open source project and I need a controller to handle a couple of forms that need to be submitted in.

I created these controllers inside a directory inside the gem called app/controllers/gemname/my_controller.rb.

However, when I try to access the controller, it seems not to be loaded (I get a name error just as if I typed something like NonExistentController).

How do I load my controller with the gem?

Thanks!

Rimian
  • 36,864
  • 16
  • 117
  • 117
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
  • 1
    Is your `MyController` defined inside `Gemname` module? If it's not rails will expect it to be placed in `app/controllers/my_controller.rb`. – KL-7 Dec 31 '11 at 16:29
  • Please read this guide http://guides.rubyonrails.org/plugins.html – taro Dec 31 '11 at 16:48
  • KL-7, how do I define MyController inside of the Gemname module? taro, thanks for the info. I did, that doesn't contain the information I'm looking for though. – Yuval Karmi Dec 31 '11 at 18:13

2 Answers2

10

Let's assume your gem is called MyGem and you have a controller called SuperController that you want to use in the app. Your controller should be defined as:

module MyGem
  class SuperController < ApplicationController
    def whatever
      ...
    end
  end
end

and in your gem directory it should live at app/controllers/my_gem/super_controller.rb (not under the lib folder). Check out the source for Devise as they do the same thing.

[Edit] You may learn something from A Guide To Starting Your Own Rails Engine Gem regarding your current project.

casraf
  • 21,085
  • 9
  • 56
  • 91
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
0

The guide in Brandon's answer is very helpful but only applies to rails 3.0. Since 3.1 you can create a plugin. Like this: rails plugin new my_engine --mountable

See this helpful guide:
http://namick.tumblr.com/post/17663752365/how-to-create-a-gemified-plugin-with-rails-3-2-rspec
(> Rails 3.0)

Rails official guide (edge):
http://edgeguides.rubyonrails.org/engines.html
http://edgeguides.rubyonrails.org/plugins.html

Old enginex:
https://github.com/josevalim/enginex
(3.0 only)

Rimian
  • 36,864
  • 16
  • 117
  • 117