从ActiveAdmin(RoR)中的collection_action调用类

时间:2012-11-07 15:26:19

标签: ruby-on-rails

我是铁轨上的红宝石新手。我正在尝试从ActiveAdmin中的collection_action调用一个类。这是代码(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来打电话给班级但是没有工作,我也没有任何关于如何做的线索。任何的想法?谢谢!

1 个答案:

答案 0 :(得分: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

您可以轻松地将其改编为您的代码