Ruby / Rails discrepancies

Hey,

I'm VERY new to Ruby on Rails. I was wondering if somebody could help
me out by clearing up a few things. I've been noticing that stuff I try
in Ruby will work fine, but the same code in a Rails app won't work. So
let me post a few lines of code here, and maybe (hopefully) somebody
can help me figure out why it works in a ruby shell thingy and not in a
rails app.

Here's the Ruby:

require 'mechanize'

agent = WWW::Mechanize.new

agent.user_agent_alias = 'Windows IE 6'

page =
agent.get('https://login.yahoo.com/config/login?login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')

page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//wherever.net')

p page

When this runs, it works just fine. When I try it in a rails app, as
posted below, it doesn't. I'll post the error message too.

class EnginesController < ApplicationController

require 'net/http'
require 'net/https'
require 'uri'
  require 'mechanize'

  def yahoo

  agent = WWW::Mechanize.new

agent.user_agent_alias = 'Windows IE 6'

page = agent.get('https://login.yahoo.com/config/login?
login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')

page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//wherever.net')

  @data=page
  end
end

(@data is just a variable that gets send to the output page)
I then navigate to the correct page and I get this error:

undefined method `add_header' for #<Net::HTTP::Get GET>

I really don't understand. There are a few other errors that I get in
Rails that I don't understand either; a lot having to do with
databases. I'm pretty sure both my Ruby and Rails installations are
current. I greatly appreciate any help and advice you can give.

Thanks,
   Burak

Burak-

  I think what is happening is that rails is munging with the request object and causing a conflict between ActionController and WWW:::Mechanize. What I would do is move the mechanize stuff into its own model class and just call its methods from your controller. But just make your class by itself and don't inherit from ApplicationController. This way you probably won't get a conflict between the two functionalities.

Cheers-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732

···

On Nov 10, 2005, at 8:52 PM, burak@lysurge.net wrote:

Hey,

I'm VERY new to Ruby on Rails. I was wondering if somebody could help
me out by clearing up a few things. I've been noticing that stuff I try
in Ruby will work fine, but the same code in a Rails app won't work. So
let me post a few lines of code here, and maybe (hopefully) somebody
can help me figure out why it works in a ruby shell thingy and not in a
rails app.

Here's the Ruby:

require 'mechanize'

agent = WWW::Mechanize.new

agent.user_agent_alias = 'Windows IE 6'

page =
agent.get('https://login.yahoo.com/config/login?login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')

page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//wherever.net')

p page

When this runs, it works just fine. When I try it in a rails app, as
posted below, it doesn't. I'll post the error message too.

class EnginesController < ApplicationController

require 'net/http'
require 'net/https'
require 'uri'
  require 'mechanize'

  def yahoo

  agent = WWW::Mechanize.new

agent.user_agent_alias = 'Windows IE 6'

page = agent.get('https://login.yahoo.com/config/login?
login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')

page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//wherever.net')

  @data=page
  end
end

(@data is just a variable that gets send to the output page)
I then navigate to the correct page and I get this error:

undefined method `add_header' for #<Net::HTTP::Get GET>

I really don't understand. There are a few other errors that I get in
Rails that I don't understand either; a lot having to do with
databases. I'm pretty sure both my Ruby and Rails installations are
current. I greatly appreciate any help and advice you can give.

Thanks,
   Burak

Are the requires really within your class, as your message shows? Try
moving them above the class declaration and see if that helps.

Jim

burak@lysurge.net wrote:

···

Hey,

I'm VERY new to Ruby on Rails. I was wondering if somebody could help
me out by clearing up a few things. I've been noticing that stuff I try
in Ruby will work fine, but the same code in a Rails app won't work. So
let me post a few lines of code here, and maybe (hopefully) somebody
can help me figure out why it works in a ruby shell thingy and not in a
rails app.

Here's the Ruby:

require 'mechanize'

agent = WWW::Mechanize.new

agent.user_agent_alias = 'Windows IE 6'

page =
agent.get('https://login.yahoo.com/config/login?login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')

page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//wherever.net')

p page

When this runs, it works just fine. When I try it in a rails app, as
posted below, it doesn't. I'll post the error message too.

class EnginesController < ApplicationController

require 'net/http'
require 'net/https'
require 'uri'
  require 'mechanize'

  def yahoo

  agent = WWW::Mechanize.new

agent.user_agent_alias = 'Windows IE 6'

page = agent.get('https://login.yahoo.com/config/login?
login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')

page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//wherever.net')

  @data=page
  end
end

(@data is just a variable that gets send to the output page)
I then navigate to the correct page and I get this error:

undefined method `add_header' for #<Net::HTTP::Get GET>

I really don't understand. There are a few other errors that I get in
Rails that I don't understand either; a lot having to do with
databases. I'm pretty sure both my Ruby and Rails installations are
current. I greatly appreciate any help and advice you can give.

Thanks,
   Burak

My code now looks like this, yet still the same error. If I try taking
out the "< ApplicationController" i get an even scarier error
(undefined method `controller_path' for EnginesController:Class) so I
just left it in.

Any ideas?

require 'net/http'
require 'mechanize'

class EnginesController < ApplicationController

  def yahoo

  agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE 6'
page =
agent.get('https://login.yahoo.com/config/login?login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')
page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//www.lysurge.net')
  @data=page
  end
end

My code now looks like this, yet still the same error. If I try taking
out the "< ApplicationController" i get an even scarier error
(undefined method `controller_path' for EnginesController:Class) so I
just left it in.

Any ideas?

  Like I said before, you need to make this class a model class not a controller. And don't have it inherit from anything. Lets say you call this class YahooSearch:

Create a file called yahoo_search.rb and put it in your model directory

require 'net/http'
require 'mechanize'

class YahooSearch

  def yahoo

  agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE 6'
page =
agent.get('https://login.yahoo.com/config/login?login=xxxxx&passwd=xxxxx&tries=1&.src=srch&.lg=us&partner=&submit=Sign+In&.done=http://submit.search.yahoo.com/free/request')
page =
agent.get('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//www.lysurge.net')
  @data=page
  end
end

Now in any controller in your application you can use it like this:

class EnginesController < ApplicationController

     model :yahoo_search

   def search
       @data = YahooSearch.yahoo
   end

end

  And now you can call your search with the url http://example.com/engine/search

Make sense?

Cheers-

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com

···

On Nov 11, 2005, at 10:27 AM, burak@lysurge.net wrote:

this makes perfect sense to me, but apparently not the rails platform.
i did exactly what you suggested (even went so far as to completely
delete the old rails project and make a new one under a different name)
and now here's the error i get:

undefined method `yahoo' for YahooSearch:Class

this is beginning to frustrate me.. and i'm sure it's starting to
frustrate you too.. thanks for all your help

In the above example, yahoo is an instance method, but you're trying to
call it as a class method. You should either change it to a class
method, or create an instance of YahooClass and call yahoo on it.