【问题标题】:Custom scaffolding rails: Add a method in every model class自定义脚手架栏杆:在每个模型类中添加一个方法
【发布时间】:2013-10-16 13:01:06
【问题描述】:

在 Rails 3.2 中,我们如何覆盖默认模型脚手架生成器,以便在脚手架时在 model.rb 文件中添加自定义方法。

我只是希望所有模型在创建脚手架后都具有以下方法。

def code
   "some_prefix" + self.id.to_s
end

我正在尝试做类似于Override default scaffold generator in rails 3的事情

我无法确定我需要覆盖哪个文件。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    您必须覆盖 activerecord 的model.rb 文件。它看起来像这样:

    <% module_namespacing do -%>
    class <%= class_name %> < <%= parent_class_name.classify %>
    <% attributes.select(&:reference?).each do |attribute| -%>
      belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
    <% end -%>
    <% if attributes.any?(&:password_digest?) -%>
      has_secure_password
    <% end -%>
    end
    <% end -%>
    

    放到lib文件夹中:

    |____lib
    | |____templates
    | | |____active_record
    | | | |____model
    | | | | |____model.rb
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多