从控制器内运行构造函数

发布于 2024-11-08 00:44:13 字数 1170 浏览 0 评论 0原文

所以我尝试从控制器内运行生成器 我的想法是,我可以从 Rails 应用程序中生成一个脚手架

以用于测试目的,我创建了一个小型生成器,它创建了一个没有实际内容的初始化程序。我已经从 shell 中运行了这个(rails 生成初始值设定项),

lib/generators/initializer_generator.rb
    class InitializerGenerator < Rails::Generators::Base
      def create_initializer_file
        create_file "config/initializers/initializer.rb", "# Add initialization content here"
      end
    end

但是当我尝试从控制器运行生成器时出现问题

class GeneratorController < ApplicationController
  include Rails::Generators
  include Rails::Generators::Actions

  def index
    generate(:initializer)
  end

end

,因此这会带来响应“未定义的方法‘行为’” 然后我做了一些研究,知道轨道发电机是基于雷神 发现行为方法是Thor::Actions模块中的thor方法 http://rubydoc.info/github/wycats/thor/master/Thor /动作:行为

所以我更改了控制器代码以包含该模块:

class GeneratorController < ApplicationController
    include Rails::Generators
    include Rails::Generators::Actions
    include Thor::Actions

  def index
    generate(:initializer)
  end

end

现在错误是“参数数量错误(3 代表 0)”。我现在很困惑可能出了什么问题。

So I'm attempting to run a generator from within a controller
the idea being that i could generate a scaffold from within a rails application

for testing purposes ive created a small generator that creates an initializer with no real content. i've run this from within the shell (rails generate initializer)

lib/generators/initializer_generator.rb
    class InitializerGenerator < Rails::Generators::Base
      def create_initializer_file
        create_file "config/initializers/initializer.rb", "# Add initialization content here"
      end
    end

but the problem comes in when i attempt to run the generator from a controller

class GeneratorController < ApplicationController
  include Rails::Generators
  include Rails::Generators::Actions

  def index
    generate(:initializer)
  end

end

so this bring the response 'undefined method `behavior'
then i did some research knowing that rails generators are based apon Thor
and found that the behavior method is a thor method in the Thor::Actions modules
http://rubydoc.info/github/wycats/thor/master/Thor/Actions:behavior

So I changed the controller code to include that module:

class GeneratorController < ApplicationController
    include Rails::Generators
    include Rails::Generators::Actions
    include Thor::Actions

  def index
    generate(:initializer)
  end

end

Now the error is 'wrong number of arguments (3 for 0)'. Am very confused now about what might be going wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-11-15 00:44:13

您是否尝试过直接给班级打电话?

InitializerGenerator.new.initializer

这是 Thor 相对于 Rake 的优势,这些都是常规的 Ruby 类。

我必须在我的方法中设置destination_root,例如:

  def create_initializer_file
    destination_root = Rails.root
    create_file "config/initializers/initializer.rb", "# Add initialization content here"
  end

我刚刚开始使用生成器,所以可能有更好的方法。

此后我遇到了一个尚未解决的问题,那就是如果文件已经存在,Thor 会提示用户输入。

Have you tried calling the class directly?

InitializerGenerator.new.initializer

This is an advantage of Thor over Rake, these are regular Ruby classes.

I had to set the destination_root inside my method, example:

  def create_initializer_file
    destination_root = Rails.root
    create_file "config/initializers/initializer.rb", "# Add initialization content here"
  end

I've only just started using generators so there might be a better way.

One issue I ran in to after this, which I have yet to solve is that Thor prompts for user input if the file already exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文