Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ruby / Rails discrepancies

0 views
Skip to first unread message

bu...@lysurge.net

unread,
Nov 10, 2005, 11:49:07 PM11/10/05
to
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

Ezra Zygmuntowicz

unread,
Nov 11, 2005, 12:37:12 AM11/11/05
to

> 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
ez...@yakima-herald.com
509-577-7732


jeem

unread,
Nov 11, 2005, 10:51:12 AM11/11/05
to
Are the requires really within your class, as your message shows? Try
moving them above the class declaration and see if that helps.

Jim

bu...@lysurge.net

unread,
Nov 11, 2005, 1:23:01 PM11/11/05
to
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('http://submit.search.yahoo.com/free/request?class=Submit&pass=1&url=http%3A//www.lysurge.net')
@data=page
end
end

Ezra Zygmuntowicz

unread,
Nov 11, 2005, 4:28:08 PM11/11/05
to

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

> 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
ez...@yakima-herald.com

bu...@lysurge.net

unread,
Nov 13, 2005, 10:02:11 PM11/13/05
to
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

Ken Kunz

unread,
Nov 14, 2005, 4:47:30 PM11/14/05
to
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.

0 new messages