Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将分页XML(plist)数据插入Rails中的数据库?_Ruby On Rails_Xml_Ruby On Rails 3_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails 如何将分页XML(plist)数据插入Rails中的数据库?

Ruby on rails 如何将分页XML(plist)数据插入Rails中的数据库?,ruby-on-rails,xml,ruby-on-rails-3,mongodb,mongoid,Ruby On Rails,Xml,Ruby On Rails 3,Mongodb,Mongoid,我从一个外部API获取了大量XML数据 它给我所有的数据分页,每页有20条记录 它还提供了每个文档的总记录计数 我用typhousgem获取XML,实际上是plist数据,我用plistgem转换它,并将它们插入数据库 问题是,;我可以很容易地插入前20条记录,也就是第一页。但是,如何计算每个文档有多少页,如何动态查询其他页 这是我的控制器和操作。它适用于第一页,但不适用于其他页 class Admin::VideosController < ApplicationController d

我从一个外部API获取了大量XML数据

它给我所有的数据分页,每页有20条记录

它还提供了每个文档的总记录计数

我用
typhous
gem获取XML,实际上是plist数据,我用
plist
gem转换它,并将它们插入数据库

问题是,;我可以很容易地插入前20条记录,也就是第一页。但是,如何计算每个文档有多少页,如何动态查询其他页

这是我的控制器和操作。它适用于第一页,但不适用于其他页

class Admin::VideosController < ApplicationController
def index


      @videos = Video.where(program_id: params[:p_id])
      @program = Program.find(params[:p_id])


    if params[:cmd]=="get_videos_from_outer"
        @page=1
        @fetch = Typhoeus::Request.post("URL", :params => {:commandtype=>"getprogramepisodes", :feedtype=>"plist",:id=>@program.kid, :page=>@page})
        @plist = Plist::parse_xml(@fetch.body.force_encoding 'utf-8')

        import_outer_videos(@plist)
        redirect_to :action=>"index", :p_id=>params[:p_id]

    end


  end
end
PS:我正在使用MongoDB和Mongoid


提前谢谢你的帮助。

我已经自己解决了。这是我为任何可能需要做类似事情的人提供的解决方案

def get_videos_from_outer(page=params[:page], kid=params[:kid], totalCount="")

        @videos = Video.where(program_id: params[:p_id])
        @program = Program.find(params[:p_id])

        @fetch = Typhoeus::Request.post("URL", :params => {:commandtype=>"getprogramepisodes", :feedtype=>"plist",:id=>kid.to_i, :page=>page.to_i})
        @plist = Plist::parse_xml(@fetch.body.force_encoding 'utf-8')



          @totalCount = @plist.second.first['totalCount']

          if !totalCount.blank?
            @totalCount = totalCount
          end
        import_outer_videos(@plist, kid, page.to_i, @totalCount.to_i)

  end
以及
导入外部视频
方法

private

def import_outer_videos(plist, kid, page, totalCount)

         @totalCount = totalCount

        plist.second.each_with_index do |t, i| 
        # First page has odd data and here we're getting rid off them
                if page.to_i==1
                   if i > 0
                   @new = Video.create(:thumb_path=>t['tnPath'], :vid=>t['id'], :title=>t['title'], :type=>t['type'], :kid=>kid, :program_id=>@program.id)

                    end
                else
                  @new = Video.create(:thumb_path=>t['tnPath'], :vid=>t['id'], :title=>t['title'], :type=>t['type'], :kid=>kid, :program_id=>@program.id)       

                end

        end

        if page.to_i < (@totalCount.to_i/20) + 1
        page = page.to_i + 1 

        get_videos_from_outer(page.to_i, kid.to_i, @totalCount)
        else
          redirect_to :action=>"index", :p_id=>params[:p_id]
        end

          if @new.errors.blank?
          flash[:notice]="#{@totalCount} videos has been transfered."
          else
            flash[:notice]="No new video."
          end
end
private
def导入外部视频(plist、kid、page、totalCount)
@totalCount=totalCount
plist.second.每个带有指数do | t,i |
#第一页有奇怪的数据,这里我们要去掉它们
如果page.to_i==1
如果i>0
@新建=视频。创建(:thumb_path=>t['tnPath'],:vid=>t['id'],:title=>t['title'],:type=>t['type'],:kid=>kid,:program_id=>@program.id)
结束
其他的
@新建=视频。创建(:thumb_path=>t['tnPath'],:vid=>t['id'],:title=>t['title'],:type=>t['type'],:kid=>kid,:program_id=>@program.id)
结束
结束
如果page.to_i<(@totalCount.to_i/20)+1
第页=第页至第i+1页
从外部获取视频(page.to\u i,kid.to\u i,@totalCount)
其他的
重定向到:action=>“index”,:p_id=>params[:p_id]
结束
如果@new.errors.blank?
flash[:注意]=“#{@totalCount}视频已传输。”
其他的
flash[:注意]=“没有新视频。”
结束
结束
private

def import_outer_videos(plist, kid, page, totalCount)

         @totalCount = totalCount

        plist.second.each_with_index do |t, i| 
        # First page has odd data and here we're getting rid off them
                if page.to_i==1
                   if i > 0
                   @new = Video.create(:thumb_path=>t['tnPath'], :vid=>t['id'], :title=>t['title'], :type=>t['type'], :kid=>kid, :program_id=>@program.id)

                    end
                else
                  @new = Video.create(:thumb_path=>t['tnPath'], :vid=>t['id'], :title=>t['title'], :type=>t['type'], :kid=>kid, :program_id=>@program.id)       

                end

        end

        if page.to_i < (@totalCount.to_i/20) + 1
        page = page.to_i + 1 

        get_videos_from_outer(page.to_i, kid.to_i, @totalCount)
        else
          redirect_to :action=>"index", :p_id=>params[:p_id]
        end

          if @new.errors.blank?
          flash[:notice]="#{@totalCount} videos has been transfered."
          else
            flash[:notice]="No new video."
          end
end