Hopefully this is a basic rails question. I’m just learning the broad strokes.
I am using rails 3.2.8 with Mechanize 2.5.1 to screen scrape and I’m using Mechanize’s basic tutorial model for scraping Google.com (here: http://mechanize.rubyforge.org/GUIDE_rdoc.html ). In the console it works like a charm. I get the full list of google page links. Now I want that list on my web page. The way I’m hacking on it, my instance variable either prints the first or last link only because its ether just running for one link, or being re-assigned each run (if I understand correctly). I am not currently using a model b/c it not hitting my db (is that wrong?). Only the controller and view with the route…
“views/listing/results.html.erb” includes:
<ul>
<li><%= link_to @listings %></li>
</ul>
“controllers/listing_controller.rb” has only:
class ListingController < ApplicationController
def results
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
page = agent.get('http://google.com')
page.links.each do |link| # for each link print out the link.
puts link.text
@listings = link.text # this is getting re-assigned each time so ends up as last one
# return @listings # returns first item on list
end
end
end
“config/routes.rb” file includes:
match '/listing/results' => 'listing#results', :as => 'listing_button'
I know there’s a way to do this but I must be searching in all the wrong places for an answer. I’ve tried several different scenarios with assigning the instance variable but maybe I’m doing this all wrong, I donno.
Thanks
Mike
1 Answer