0

我的 Rails 应用程序中有一个模型“Geo”,如下所示。我在 Rails 应用程序中将 text.txt 文件保存在哪里以及在特定代码行中调用什么地址 File.open(" /india/text.txt")

注意:text.txt 是包含 txt 文件的数据。

 class Geo < ActiveRecord::Base
      attr_accessible :country_iso2, :country_name, :district, :latitude, :locality, :longitude, :state_iso2, :state_name, :street, :taluka, :zip

    def self.create_or_save(locality_s, zip_s, state_s, district_s)
        g = Geo.where(zip: zip_s).first
        g = Geo.new(zip: zip_s) if g.blank?
        g.locality = locality_s
        g.state_name = state_s
        g.district = district_s
        g.save
    end

    def self.mine
        File.open("/india/text.txt") do |f|
            f.each_line do |line|
                li = line.split(":")
                    locality = li[0].to_s.strip
                    zip = li[1].to_s.strip
                    state = li[2].to_s.strip
                    district = li[3].to_s.strip
                Geo.create_or_save(locality, zip, state, district)
            end 
        end
    end
end
4

1 回答 1

0

我想你想使用Rails.root

Rails.root.join('app', 'assets', 'txt', 'india', 'text.txt')

映射到 /app/assets/txt/india/text.txt

于 2013-05-26T19:20:20.053 回答