0

I am following railscasts tokenfield revised @258 video. Looks like it uses rails 3.

While working on it, I got stuck after I setup the has many through association and get the authors ids split by ','. I am using rails 4.2.8 version.

Error:

NoMethodError in Books#new

Showing /home/nicholas/labs/tokenfield-258/app/views/books/_form.html.erb where line #21 raised:

undefined method `author_tokens' for #

_form.html.erb

  <div class="field">
    <%= f.label :author_tokens, "Authors" %><br />
    <%= f.text_field :author_tokens, data: {load: @book.authors} %>
  </div>

books_controller.rb

def book_params
   params.require(:book).permit(:name,:author_tokens)
end

book.rb

class Book < ActiveRecord::Base
  has_many :bookauthors
  has_many :authors, through: :bookauthors

  def author_tokens=(ids)
    self.author_ids = ids.split(',')
  end

end

I am getting error message as undefined method `author_tokens' for # created_at: nil, updated_at: nil>

Could anyone help me how I can fix this issue? I am using rails 4.2.8 version.

nekolus
  • 33
  • 6

1 Answers1

0

Missed this line attr_reader :author_tokens book.rb. Now it works.

class Book < ActiveRecord::Base
  has_many :bookauthors
  has_many :authors, through: :bookauthors

  attr_reader :author_tokens

  def author_tokens=(ids)
    self.author_ids = ids.split(",")
  end

end
nekolus
  • 33
  • 6