Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在构建自定义生成器时使用partials_Ruby On Rails_Ruby_Erb - Fatal编程技术网

Ruby on rails 在构建自定义生成器时使用partials

Ruby on rails 在构建自定义生成器时使用partials,ruby-on-rails,ruby,erb,Ruby On Rails,Ruby,Erb,我正在构建自定义生成器以生成rails模型。生成器可以使用多个模板中的一个来构建一个模型。某些逻辑在所有模板中都是相同的 是否可以将模板的某些部分提取为部分 下面是我想做的一个例子: lib/generators/custom\u model/custom\u model\u generator.rb class CustomModelGenerator < Rails::Generators::Base source_root File.expand_path('../templat

我正在构建自定义生成器以生成rails模型。生成器可以使用多个模板中的一个来构建一个模型。某些逻辑在所有模板中都是相同的

是否可以将模板的某些部分提取为部分

下面是我想做的一个例子:

lib/generators/custom\u model/custom\u model\u generator.rb

class CustomModelGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  argument :model_name, type: :string
  argument :model_type, type: :string
  ...

  include GeneratorsHelper

  def generate_model
    template_path =
      case model_type
      when 'car' then 'car_model.rb.erb'
      when 'plane' then 'plane_model.rb.erb'
      ...
      end

    template template_path, "app/models/#{model_name}.rb"
  end
end

#start
方法也将用于其他模型生成器。我想把它提取成一个部分。有可能吗?

当然有可能,但我不认为有任何内在因素。ActionView不会呈现模板。您可能会编写类似于helper的方法来调用
template template\u path,path\u to\u partial
。这当然是可能的,但我认为没有内置任何东西。ActionView不会呈现模板。您可能会编写一个类似于helper的方法来调用
template-template\u-path,path-to-u-partial
class <%= model_name.camelcase %> < ApplicationRecord
  def start
    puts "Vroum!"
  end
end