Hey guys… having an issue that hopefully someone can help with.
I have a ActiveRecord model called WebPage, it has two fields - url and title. I want the title to be determined by parsing the html (I’m using Nokogiri) - and this is where I’m having issues.
My code looks something like:
class WebPage < ActiveRecord::Base
attr_accessible :url, :title, :doc
@doc ||= Nokogiri::HTML(open(@url))
title = @doc.css(‘title’)
What is happening, is if I run this and try and create a new object (@page = WebPage.new(@url), I get:
ActionView::Template::Error (undefined method `css’ for nil:NilClass)
Now, if I set @doc in my controller, and change the name of title to set_title and call @page.title = @page.set_title, it works. But that is very ugly and if I’ve learned anything from rails, is that if it looks ugly to start with, it’s probably not the right way.
What am I doing wrong?