2

我是 ruby​​ on rails 的新手。我正在尝试从collection_actionActiveAdmin 中调用一个类。这是代码(app/admin/models):

collection_action :status_race, :method => :post do
    #Do some import work..
    redirect_to :class => :import_route
end 

这是我要调用的类的代码(app/lib/route):

class ImportRoute
def  initialize
    @seperator = " "
    @time_format = "%d-%m-%y"
end
def run(filename)
    puts "Running route import file"

    raise "File" + filename + "doesn't not exist" unless File.exist(filename)

    ri = RouteImporter.find(:name => self.class.name)

    if(ri.nil?)
        puts "Error, file doesn't exists"
    end
    CSV.foreach(filename, {:col_sep => @seperator}) do |row|
        if row.lenght >5
            ri.country_name = row[0] + " " + row[1]
            ri.type = row[2]
            ri.company = row [3]
        else
            ri.country_name = row[0]
            ri.type = row[1]
            ri.company = row[2]
            ri.date = row[4].gsub(";", " ")
        end
    end
end

结尾

我曾经redirect_to打电话给班级,但没有工作,而且我不知道该怎么做。任何的想法?谢谢!

4

1 回答 1

0

此代码取自http://activeadmin.info/docs/8-custom-actions.html#collection_actions

ActiveAdmin.register Post do
  collection_action :import_csv, :method => :post do
    # Do some CSV importing work here...
    redirect_to {:action => :index}, :notice => "CSV imported successfully!"
  end
end

此收集操作将在“/admin/posts/import_csv”处生成一个指向 Admin::PostsController#import_csv 控制器操作的路由。

所以这意味着你必须在 app/controllers/admin/posts_controller.rb 中添加一个方法 import_csv。在此方法中,您可以实例化您的模型:

def import_csv
  import_route = ImportRoute.new
  # do stuff on this object
end

您可以轻松地将其调整为您的代码

于 2012-11-07T18:35:42.247 回答