Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 使mechanize页面超出请求边界_Ruby On Rails_Ruby_Mechanize - Fatal编程技术网

Ruby on rails 使mechanize页面超出请求边界

Ruby on rails 使mechanize页面超出请求边界,ruby-on-rails,ruby,mechanize,Ruby On Rails,Ruby,Mechanize,我正在编写一个ruby应用程序,可以代表用户向远程博客发布评论。我的问题是,我必须在控制器的post方法中使用相同的页面,以保持会话活动并填写验证码: app/controller/comment_controller.rb 我已经尝试将页面实例变量保存在Rails.cache中,但无法将页面编组为字符串 我写了一个有效的解决方案。它将隐藏变量和cookie保存在base64编码字符串中,iam在隐藏字段中的请求之间传输该字符串。以下是构建代码的基础: require 'mechanize' r

我正在编写一个ruby应用程序,可以代表用户向远程博客发布评论。我的问题是,我必须在控制器的post方法中使用相同的页面,以保持会话活动并填写验证码:

app/controller/comment_controller.rb


我已经尝试将页面实例变量保存在Rails.cache中,但无法将页面编组为字符串

我写了一个有效的解决方案。它将隐藏变量和cookie保存在base64编码字符串中,iam在隐藏字段中的请求之间传输该字符串。以下是构建代码的基础:

require 'mechanize'
require 'stringio'
require 'base64'

class MechanizeWrapper
  attr_reader :page, :agent

  def initialize(url, useproxy = true)
    @agent = Mechanize.new
    @page = @agent.get(url)
  end

  def get_state()
    hidden_fields = {}
    cookie_jar = StringIO.new

    @page.search("//input[@type='hidden']").each do |hidden| 
      hidden_fields[hidden.path]=hidden.attribute('value').to_s
    end

    @agent.cookie_jar.dump_cookiestxt(cookie_jar);

    state = {:hidden_fields => hidden_fields.inspect, :cookie_jar => cookie_jar.string}
    Base64.encode64(state.inspect)
  end

  def put_state(state_enc)
    state = eval(Base64.decode64(state_enc))
    eval(state[:hidden_fields]).each do |path,value|  
      @page.search(path).first['value'] = value
    end    

    cookie_jar = StringIO.new(state[:cookie_jar])
    @agent.cookie_jar.load_cookiestxt(cookie_jar)
  end
end

这是rails控制器吗?为什么您认为@page会在请求之间保持不变?还是那个问题的代理人?有一个“新”的方法似乎是个好主意吗?我认为您对ruby和/或rails缺乏一些基本的理解。@pguardiario:我认为请求之间的'@page'不会持续存在,请参阅agent.submit行注释,这是问题的核心。新方法是rails生成的脚手架。
require 'mechanize'
require 'stringio'
require 'base64'

class MechanizeWrapper
  attr_reader :page, :agent

  def initialize(url, useproxy = true)
    @agent = Mechanize.new
    @page = @agent.get(url)
  end

  def get_state()
    hidden_fields = {}
    cookie_jar = StringIO.new

    @page.search("//input[@type='hidden']").each do |hidden| 
      hidden_fields[hidden.path]=hidden.attribute('value').to_s
    end

    @agent.cookie_jar.dump_cookiestxt(cookie_jar);

    state = {:hidden_fields => hidden_fields.inspect, :cookie_jar => cookie_jar.string}
    Base64.encode64(state.inspect)
  end

  def put_state(state_enc)
    state = eval(Base64.decode64(state_enc))
    eval(state[:hidden_fields]).each do |path,value|  
      @page.search(path).first['value'] = value
    end    

    cookie_jar = StringIO.new(state[:cookie_jar])
    @agent.cookie_jar.load_cookiestxt(cookie_jar)
  end
end