Im writing a ruby application that can post comments on behalf of the user to a remote blog. My problem is that i have to use the same page in the post method of the controller, to keep the session alive & to fill out a captcha:
app/controller/comment_controller.rb
require 'mechanize'
class CommentController < ApplicationController
def new
agent = Mechanize.new
@page = agent.get('http://blog.example.com')
@captcha_src = @page.search("//div[@id='recaptcha_image']").search("//img")[1].attribute("src")
#etc.
end
def post_comment
# insert captcha, username, password + text into the form
agent.submit(@page.form[0], @page.form[0].buttons.submitbutton) # Problem: page instance variable doesn't exist anymore
end
end
I've already tried to save the page-instance-variable in Rails.cache but mechanized pages can't be marshalled to string.