我在做一個網址縮短器作爲學習練習。除了訪問模式(對於這個問題不重要),它還有一個Url和一個Link模型,我已經加入了這些模型。 Url類屬於鏈接,Link類has_one:url。Rails:belongs_to和has_one關聯創建的方法
總之,問題是,當我稱之爲Link類的方法縮短(從創建在URL控制器方法),我收到此錯誤信息
undefined method `link' for #<ActiveRecord::Relation:0x00000102f648b8>
應用程序跟蹤點這條線在連接類中的「縮短」的方法(下面複製)
return url.link if url
所以,我理解這個錯誤意味着我不能叫上「鏈接」「網址」。但是,我認爲我創建的協會會允許我這樣做。你能解釋一下我做錯了
相關的代碼在
形成新
<%= simple_form_for @url do |f| %>
<%= f.input :original, :label => 'Original Link', :input_html => { :maxlength => 70 } %>
<%#= f.input :custom, :label => '(Optional) Create your own custom shortened link ' %>
<%= f.button :submit %>
<% end %>
創建方法URL控制器
def create
@url = Url.new(params[:url])
@link = Link.shorten(@url)
respond_to do |format|
if @url.save
format.html { redirect_to action: "index", notice: 'Url was successfully created.' }
format.json { render json: @url, status: :created, location: @url }
else
format.html { render action: "new" }
format.json { render json: @url.errors, status: :unprocessable_entity }
end
end
end
URL類
class Url < ActiveRecord::Base
attr_accessible :original
belongs_to :link
end
鏈接類縮減的方法
class Link < ActiveRecord::Base
attr_accessible :identifier
has_one :url
has_many :visits
def self.shorten(original, custom=nil)
url = Url.find_by_original(original)
return url.link if url #this is the problem line
link = nil
if custom
raise 'Someone has already taken this custom URL, sorry' unless Link.find(:identifier => custom).nil? #this Link.find
raise 'This custom URL is not allowed because of profanity' if DIRTY_WORDS.include? custom
transaction do |txn|
link = Link.new(:identifier => custom)
link.url = Url.create(:original => original)
link.save
end
else
transaction do |txn|
link = create_link(original)
end
end
return link
end