Ruby on rails Rails编辑脚手架生成器

Ruby on rails Rails编辑脚手架生成器,ruby-on-rails,edit,scaffold,Ruby On Rails,Edit,Scaffold,我正在尝试编辑rails脚手架生成器,我想在其中向控制器添加自定义操作,并将它们添加到路由中。我可以通过在我的lib目录中添加以下代码来编辑模型生成器: #/lib/template/active_record/model.rb <% module_namespacing do -%> class <%= class_name %> < <%= parent_class_name.classify %> def self.import

我正在尝试编辑rails脚手架生成器,我想在其中向控制器添加自定义操作,并将它们添加到路由中。我可以通过在我的lib目录中添加以下代码来编辑模型生成器:

 #/lib/template/active_record/model.rb
      <% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>

  def self.import1(file)

    spreadsheet = Roo::Spreadsheet.open(file.path)
          header = spreadsheet.row(1)
          (2..spreadsheet.last_row).each do |i|
            row = Hash[[header, spreadsheet.row(i)].transpose]
            puts row.to_hash
            product = find_by(id: row["id"]) || new
            product.attributes = row.to_hash
            product.save!
          end

  end

<% attributes.select(&:reference?).each do |attribute| -%>
  belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
<% end -%>
<% attributes.select(&:token?).each do |attribute| -%>
  has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
<% end -%>
<% if attributes.any?(&:password_digest?) -%>
  has_secure_password
<% end -%>
end
<% end -%>
我需要在我的lib文件夹中写入或复制什么,它可以通过以下两个操作生成控制器:

  def import
   # Module1.import(params[:file])
   ProductionProductivity7.import1(params[:file])
   redirect_to tests_path, notice: "Products imported."
  end
在我的路线中,我希望添加如下内容:

 resources :production_productivity9s do
     collection { post :import }
     collection { get :dropdown }
     collection { get :test }
  end

而不是资源:生产效率。我想要一个解决方案来编辑控制器和路由。我正在网上搜索,没有找到任何解决方案

我试图编辑资源路由并向我的控制器添加一些操作我添加了以下用于编辑路由的代码:

#rails/generators/rails/resource_route

module Rails
  module Generators
  class ResourceRouteGenerator < NamedBase # :nodoc:
    # Properly nests namespaces passed into a generator
    #
    #   $ rails generate resource admin/users/products
    #
    # should give you
    #
    #   namespace :admin do
    #     namespace :users do
    #       resources :products
    #     end
    #   end
    def add_resource_route
      return if options[:actions].present?

      depth = 0
      lines = []

      # Create 'namespace' ladder
      # namespace :foo do
      #   namespace :bar do
      regular_class_path.each do |ns|
        lines << indent("namespace :#{ns} do\n", depth * 2)
        depth += 1
      end

      # inserts the primary resource
      # Create route
      #     resources 'products'
      #  lines << indent(%{resources :#{file_name.pluralize}\n }, depth * 2) 
      lines << indent(%{resources :#{file_name.pluralize} do 
                collection { post :import }
                collection { get :dropdown }
                collection { get :test }
                end \n}, depth * 2)

      # Create `end` ladder
      #   end
      # end
      until depth.zero?
        depth -= 1
        lines << indent("end\n", depth * 2)
      end

      route lines.join
    end
  end
end
#轨道/发电机/轨道/资源#路线
模块导轨
模块生成器
类ResourceRouteGenerator#rails/generators/rails/resource_route

module Rails
  module Generators
  class ResourceRouteGenerator < NamedBase # :nodoc:
    # Properly nests namespaces passed into a generator
    #
    #   $ rails generate resource admin/users/products
    #
    # should give you
    #
    #   namespace :admin do
    #     namespace :users do
    #       resources :products
    #     end
    #   end
    def add_resource_route
      return if options[:actions].present?

      depth = 0
      lines = []

      # Create 'namespace' ladder
      # namespace :foo do
      #   namespace :bar do
      regular_class_path.each do |ns|
        lines << indent("namespace :#{ns} do\n", depth * 2)
        depth += 1
      end

      # inserts the primary resource
      # Create route
      #     resources 'products'
      #  lines << indent(%{resources :#{file_name.pluralize}\n }, depth * 2) 
      lines << indent(%{resources :#{file_name.pluralize} do 
                collection { post :import }
                collection { get :dropdown }
                collection { get :test }
                end \n}, depth * 2)

      # Create `end` ladder
      #   end
      # end
      until depth.zero?
        depth -= 1
        lines << indent("end\n", depth * 2)
      end

      route lines.join
    end
  end
end
 #/lib/templates/rails/scaffold_controller/controller.rb
   def import
  <%= class_name %>.import1(params[:file])
  redirect_to tests_path, notice: "Products imported."
 end