我有一个包含源 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
hostname=@url.split('/')[2]
hostname["www."]=""
hostname
end
end
我在 indexview 中使用它
<a href="<%=item.url%>"><%=item.source %></a>
所以我可以很好地访问 item.url。但 item.source 抛出异常:
nil:NilClass 的未定义方法“split”
我认为这是因为源方法没有访问 url 属性。我如何让它做到这一点?我假设这就是它应该如何在红宝石中工作。可能是我犯了一个错误