SoWhat
SoWhat

Reputation: 5622

Method on ruby model not accessing property

I have a model item which contains a source url

class Item < ActiveRecord::Base
    attr_accessible :name, :price, :priority,:picture,:url
    belongs_to :user

    validates :url, format: /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
    validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/png']

    #default_scope {where "completed<>trued"}

    def source
            [email protected]('/')[2]
            hostname["www."]=""
            hostname


    end

end

and I use this in the indexview

    <a href="<%=item.url%>"><%=item.source %></a>

So I can access item.url fine. but item.source throws an exception:

undefined method `split' for nil:NilClass

I think this is because source method is not accessing the url property. How do I make it do that? I am assuming this is how its supposed to work in ruby. Might be I am making a blunder

Upvotes: 0

Views: 36

Answers (2)

Amar
Amar

Reputation: 6942

Remove @url try self.url. or simply url. because it's object attributes.

Upvotes: 1

Miotsu
Miotsu

Reputation: 1776

Where does @url come from? Maybe you mean:

self.url

Upvotes: 2

Related Questions