I sadly encounter the problem, that when I try to create a Review the Product_id stays nil. Whats the cleanest way to fill this in for the product that I want to review?
Reviews_controller
def create
@user = current_user
@review = @user.reviews.new(params[:review])
if @review.save
redirect_to products_path, notice: 'Thank you for your review'
else
render 'new'
end
end
My Product, Review and User are set up with the has_many and belongs_to relationships.
The New View for the Review
%h1 Write a Review
= form_for @review do |f|
%fieldset
= f.label :review
= f.text_area :review
%fieldset
= f.label :rating
= f.text_field :rating
%fieldset
= f.submit 'Post Review', class: 'btn btn-large btn-primary'
I’m aware that I’m not finding the Product – How do I get to that?
Since you are creating the object through an association use build
that will extract the user’s id and add it to user_id in your reviews object. I need more information to find the product, you can most likely add a hidden_field in your review form that will have the Products id