Rails Generator传递一组参数

时间:2015-06-16 01:37:36

标签: ruby-on-rails ruby code-generation simple-form

我已经看过关于如何创建自己的生成器的rails演员,并且已经阅读了许多堆栈溢出问题。基本上我要做的是构建一个类似于内置rails迁移生成器的生成器,它接受attribute:type之类的参数,然后根据需要插入它们。这是我现在为我的生成器编写的代码。

class FormGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('../templates', __FILE__)
  argument :model_name, :type => :string
  argument :attributes, type: :array, default: [], banner: "attribute:input_type attribute:input_type"
  argument :input_types, type: :array, default: [], banner: "attribute:input_type attribute:input_type"



  def create_form_file
    create_file "app/views/#{model_name.pluralize}/_form.html.erb", "


<%= simple_form_for @#{model_name.pluralize} do | f | %>
<%= f.#{input_type}  :#{attribute}, label: '#{attribute}' %>
<% end %>
"
  end
end

基本上我想要它做的是生成一个视图文件,其中包含与参数一样多的行。因此传递rails g form products name:input amount:input other_attribute:check_box会生成一个_form.html.erb文件,其中包含以下内容:

<%= simple_form_for @products do | f | %>
    <%= f.input  :name, label: 'name' %>
    <%= f.input  :amount, label: 'amount' %>
    <%= f.check_box  :other_attribute, label: 'other_attribute' %>
    <% end %>

如何编写生成器以获取多个参数并根据这些参数生成多行?

1 个答案:

答案 0 :(得分:0)

构建表单生成器:$ rails g generator form

# lib/generators/form/form_generator.rb
class FormGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('templates', __dir__)

  argument :model_name, type: :string, default: 'demo'
  class_option :attributes, type: :hash, default: {}

  def build_form
    create_file "app/views/#{model_name.pluralize}/_form.html.erb", 
    "<%= simple_form_for @#{model_name.pluralize} do | f | %>" + "\n" +
    options.attributes.map { |attribute, type|
      "<%= f.#{type}  :#{attribute}, label: '#{attribute}' %> \n"
    }.join('') +
    "<% end %>"
  end
end

测试

$ rails g form demo --attributes name:input, pass:input, remember_me:check_box

结果

# app/views/demos/_form.html.erb
<%= simple_form_for @demos do | f | %>
<%= f.input,  :name, label: 'name' %> 
<%= f.input,  :pass, label: 'pass' %> 
<%= f.check_box  :remember_me, label: 'remember_me' %> 
<% end %>