如何在自定义控制器生成器中获取GeneratedAttribute?

发布于 2024-11-14 07:25:21 字数 767 浏览 2 评论 0原文

我正在创建一个派生自 Rails::Generators::NamedBase 的自定义控制器生成器,它创建给定特定模型名称(例如 Person)的控制器和视图。我还想创建一个 _form.html.haml 部分,它根据模型的属性构建表单(我正在使用 simple_form 顺便说一句)。

到目前为止我所得到的是:

<% attributes = file_name.capitalize.constantize.columns.map { |c| [Rails::Generator::GeneratedAttribute.new(c.name, c.type)]} %>
- simple_form_for [:admin,@<%=file_name%>] do |f|
  = render 'shared/error_summary', :object => f.object
  .inputs
  <%- attributes.each do |attribute| -%>
    = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
  <%- end -%>  
  .actions
    = f.button :submit

我收到一个“未初始化的常量 Rails::Generator (NameError)”异常。不确定我需要什么,或者我上面的方法是否正确。

任何帮助都会很棒。

谢谢-wg

I'm creating a custom controller generator that derives from Rails::Generators::NamedBase that creates both a controller and views given a particular model name (e.g. Person). I also want to create a _form.html.haml partial that builds a form based on the model's attributes (I'm using simple_form btw).

What I have so far is:

<% attributes = file_name.capitalize.constantize.columns.map { |c| [Rails::Generator::GeneratedAttribute.new(c.name, c.type)]} %>
- simple_form_for [:admin,@<%=file_name%>] do |f|
  = render 'shared/error_summary', :object => f.object
  .inputs
  <%- attributes.each do |attribute| -%>
    = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
  <%- end -%>  
  .actions
    = f.button :submit

I'm getting an "uninitialized constant Rails::Generator (NameError)" exception. Not sure what I need to require or if my approach above is even right.

Any help would be awesome.

Thanks -wg

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

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

发布评论

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

评论(1

镜花水月 2024-11-21 07:25:21

我怀疑问题是你在 Generator 后面缺少了 s 。正确的方法调用是:

Rails::Generators::GeneratedAttribute.new

不要在模板中创建属性变量,最好在生成器类的初始化方法中创建它。该方法的框架如下所示:

  def initialize(*args, &block)
    super

    # Call Rails::Generators::GeneratedAttribute.new here

  end

如果您要让用户以“column_name:column_type”的形式传入所需的属性,那么您可以执行以下操作:

class FooGenerator < Rails::Generators::NamedBase
  argument :model_attributes, type: :array, default: [], banner: "model:attributes"

  def initialize(*args, &block)
    super

    @attributes = []

    model_attributes.each do |attribute|
      @attributes << Rails::Generators::GeneratedAttribute.new(*attribute.split(":")) if attribute.include?(":")
    end
  end
end

您可能还希望以某种方式处理没有传入任何属性的可能性。然而,这将取决于您的需求,因此如果没有更多信息,我无法指导您。对不起!

一个值得遵循的良好模型标准是 nifty_generators来源

I suspect the problem is that you are missing an s after Generator. The correct method call is:

Rails::Generators::GeneratedAttribute.new

Instead of creating your attributes variable inside your template, it is better to create it inside your generator class in the initialize method. This method looks like this as a skeleton:

  def initialize(*args, &block)
    super

    # Call Rails::Generators::GeneratedAttribute.new here

  end

If you are getting your user to pass in the desired attributes in the form column_name:column_type then you can do the following:

class FooGenerator < Rails::Generators::NamedBase
  argument :model_attributes, type: :array, default: [], banner: "model:attributes"

  def initialize(*args, &block)
    super

    @attributes = []

    model_attributes.each do |attribute|
      @attributes << Rails::Generators::GeneratedAttribute.new(*attribute.split(":")) if attribute.include?(":")
    end
  end
end

You might want to also handle the possibility that no attributes are passed in somehow. This will depend on your needs however so I can't guide you on that without more information. Sorry!

A good model standard to follow is the nifty_generators source.

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