Question

I'm getting this error in my browser when I go to the 'edit' page of my Article :

blogger/app/views/articles/_form.html.erb

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

Extracted source (around line #20):

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

My Article model:

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

I have an Article model that has many tags. This relationship is represented through another model called taggings. Again, I get this error on my edit page of my Article model/controller. My blogger app says that there is no method error on my :tag_list method but it exists in my article model. I am obviously missing something and need help filling in this gap.

Was it helpful?

Solution

You'll need to add

   attr_reader :tag_list

to your model.

When dealing with form elements, Rails expects each element to correspond to a model attribute. In your case, you are creating a virtual attribute, but to do so, it requires the usual getters and setters. You already supplied the setter, and attr_reader will supply the getter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top