如何使用Ruby on Rails gem包含控制器? [英] How to include a controller with a Ruby on Rails gem?

查看:152
本文介绍了如何使用Ruby on Rails gem包含控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图贡献一个开源项目,我需要一个控制器来处理需要提交的几个表单。



我创建了这些控制器里面的宝石名称叫做 app / controllers / gemname / my_controller.rb



然而,当我尝试访问控制器,它似乎没有被加载(我得到一个名称错误,就像我输入类似NonExistentController的东西)。

如何加载我的控制器宝石是什么?



谢谢!

解决方案

假设您的宝石是称为 MyGem ,并且您有一个名为 SuperController 的控制器,您可以在该应用程序中使用它。您的控制器应该定义为:

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

,并且位于您的gem目录中,它应该位于 app / controllers / my_gem / super_controller.rb not lib 文件夹)。查看 Devise的源代码,因为他们也这么做。



您可以从创建自己的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!

解决方案

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.

这篇关于如何使用Ruby on Rails gem包含控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆