覆盖rails生成器以将date_select更改为date_field

时间:2019-04-30 11:36:07

标签: ruby-on-rails-5 rails-generators

我一直在尝试覆盖rails生成器(当您运行rails generate scaffold时)以日期形式生成date_field而不是date_select(日期和时间相同)时间)。映射位于railties/lib/rails/generators/generated_attribute.rb

def field_type @field_type ||= case type when :integer then :number_field when :float, :decimal then :text_field when :time then :time_select when :datetime, :timestamp then :datetime_select when :date then :date_select when :text then :text_area when :boolean then :check_box else :text_field end end

我不知道将更新后的generated_attribute.rb文件放在何处。我已经在Rails应用lib/generators/generated_attribute.rblib/templates/generators/generated_attribute.rb中尝试了无效。我没有找到有关覆盖此文件以更改行为的其他信息。

先谢谢了。使用Rails 5.2

1 个答案:

答案 0 :(得分:1)

似乎没有自动覆盖此文件。 我把我放在lib/rails/generators/generated_attribute.rb 然后,您可以在覆盖的require "#{Rails.root}/lib/rails/generators/generated_attribute.rb"的顶部添加lib/rails/generators/erb/scaffold/scaffold_generator.rb

require "#{Rails.root}/lib/rails/generators/generated_attribute.rb"
require "rails/generators/erb"
require "rails/generators/resource_helpers"

module Erb # :nodoc:
  module Generators # :nodoc:
    class ScaffoldGenerator < Base # :nodoc:

有点脏,但我希望这会有所帮助。