Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a controller with a Ruby on Rails gem?

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!

like image 564
Yuval Karmi Avatar asked Dec 31 '11 16:12

Yuval Karmi


1 Answers

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.

like image 139
Michelle Tilley Avatar answered Nov 04 '22 01:11

Michelle Tilley