简体   繁体   中英

Why am I getting undefined method `comments' in my rails app?

I have API with The Movie Database (TMDb). I want the app to review individually. I have created all the things but this error makes be sucked? How can I get rid of this?

My error is:

 NoMethodError in ReviewsController#create undefined method `comments' for #<Review:0x00000005278fa8> Did you mean? comment comment? comment= 
 @review = current_user.reviews.new(review_params.merge(movie_id: @movie.id))

 if @review.save  # I got error here!!!
   flash[:success] = "Review saved!"
   redirect_to root_path
 else
   flash[:alert] = "Woops! It seems there was an error."
   redirect_to root_path
 end

Review Params

def review_params
  params.require(:review).permit(:comment)
end

My movie.rb is:

class Movie < ApplicationRecord
    validates :title, :release_date, :released, :runtime, :popularity, :language, :budget, :average_vote, :vote_count, :tmdb_id, presence: true
    has_many :reviews
end

My review.rb is:

class Review < ApplicationRecord
  belongs_to :user
  belongs_to :movie

  validates :user, :movie, :comments, presence: true
end

My user.rb is:

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates :username, presence: true
  validates_uniqueness_of :username
  mount_uploader :avatar, AvatarUploader

  has_many :reviews
end

Your error message says:

undefined method 'comments' for #<Review:0x00000005278fa8> . Did you mean? comment comment? comment=

This syntax #<Review:0x00000005278fa8> refers to an instance of the Review class.

Check out this code:

class Review
  def show
    puts "I'm a review"
  end
end

review = Review.new
puts review

review.show
review.comments

--output:--
#<Review:0x007fa4db0a5bb8>
I'm a review

1.rb:10:in `<main>': undefined method `comments' for #<Review:0x007fa4db0a5bb8> (NoMethodError)

Does that error look familiar? The error says that there is no method named comments() defined in the Review class.

Your error even asks you

Did you mean? comment comment? comment=

ie Is your method name actually spelled 'comment', 'comment?', or 'comment='?

The reason the error occurs here:

if @review.save  # I got error here!!!

is because that line of code causes the validations that you specified in your Review class to execute:

Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation. This avoids storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.

http://guides.rubyonrails.org/active_record_validations.html

Here's an example from the rails guide:

 class Person < ApplicationRecord validates :name, presence: true end Person.create(name: "John Doe").valid? # => true Person.create(name: nil).valid? # => false 

As you can see, our validation lets us know that our Person is not valid without a name attribute.

The error looks like it is coming for the validation you have on the Review model. The line

validates :user, :movie, :comments, presence: true

is effectively going to call @review.comments.present? before attempting to actually save the record to the DB. However, the error is telling you that there is a problem with @review.comments - the method #comments doesn't exist on the @review object.

The error you have shown is also trying to give you a hint where it reads "Did you mean comment, comment?, comment=", indicating that there is a method with a similar name (the singular comment ) and maybe you have a typo. You may just need to adjust the validation in app/models/review.rb to

validates :user, :movie, :comment, presence: true

根据错误消息判断,该字段称为comment而不是comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM