Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:59:06+00:00 2026-05-28T19:59:06+00:00

Im writing a ruby application that can post comments on behalf of the user

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:59:07+00:00Added an answer on May 28, 2026 at 7:59 pm

    I wrote a working solution. It saves the hidden-variables and the cookies in a base64 encoded string which iam transferring between requests in a hidden field. Heres the code to build upon:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small Ruby command-line application that uses fileutils from the standard library
In a fit of unoriginality, I'm writing a blog application using Ruby on Rails.
I am writing an MVC 3 application that needs to allow the user to
I'm writing an application that convert data serialized in ruby (file A) to xml
I'm currently writing an application in ruby on rails that uses AWS. I see
I have an application using Ruby on Rails where a user will be providing
I'm writing a non-Rails ruby application (gasp!) and would like to be able to
Background: I'm writing a 'standard' (nothing special) web application in Ruby (not Rails) and
Say I'm writing some ruby code and I want to use the standard Date
I'm writing a ruby program that executes some external command-line utilities. How could I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.