2013-07-09 38 views
0

我得到這個錯誤在我的瀏覽器,當我去我的文章的「編輯」頁面:從NoMethod錯誤我

博客/應用/視圖/用品/ _form.html.erb

undefined method `tag_list' for #<Article:0x007ffb52130678> 

提取的源(左右線#20):

18:  <p> 
19:   <%= f.label :tag_list %><br /> 
20:   <%= f.text_field :tag_list %> 
21:  </p> 

我的文章型號:

class Article < ActiveRecord::Base 
    attr_accessible :title, :body, :tag_list, :image 
    has_many :comments 
    has_many :taggings 
    has_many :tags, through: :taggings 
    has_attached_file :image 

    def tag_list=(tags_string) 
     tag_names = tags_string.split(",").collect{ |s| s.strip.downcase }.uniq 
     new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by_name(name) } 
     self.tags = new_or_found_tags 
    end 
end 

我有一個文章模型,有很多標籤。這種關係通過稱爲標籤的另一種模式來表示。同樣,我的文章模型/控制器的編輯頁面出現此錯誤。我的博客應用說我的:tag_list方法沒有方法錯誤,但它存在於我的文章模型中。我明顯錯過了一些東西,需要填補這個空白。

+1

如果將attr_reader:tag_list添加到模型中,它有幫助嗎? – Rebitzele

+0

工作的聖潔廢話。道具給你。如果你想解釋爲什麼attr_reader:tag_list作爲答案,我很樂意給你豎起大拇指! – andy4thehuynh

+0

當然,我添加了一個解釋。讓我知道它是否合理。 – Rebitzele

回答

1

你需要

attr_reader :tag_list 

添加到您的模型。

在處理表單元素時,Rails期望每個元素都對應於屬性的屬性。就你而言,你正在創建一個虛擬屬性,但要做到這一點,它需要通常的getter和setter。你已經提供了setter,attr_reader將提供getter。