I rewrote my create function in my scaffold Review after making it associated with my Concert model. When I try to submit a form to create a review though I get an error saying
undefined method `reviews' for #Class:0xab9972c>
def create
@review = Concert.reviews.create(review_params)
end
My Concert model looks like this
class Concert < ActiveRecord::Base
validates_presence_of :artist
validates_presence_of :venue
validates_presence_of :date
has_many :reviews
end
and my Review model looks like this
class Review < ActiveRecord::Base
validates_presence_of :artist
validates_presence_of :venue
validates_presence_of :date
belongs_to :user
belongs_to :concert
end
I also added the relations within my migration files but I still get the error. Can someone explain to me what is causing this and how I could go about creating a review that belongs to a concert?
The association has_many :reviews
is an instance method. I suspect that in your create method you want something like this:
def create
@concert = Concert.new
@concert.save
@review = @concert.reviews.create(review_params)
end