I’m trying to create a “review” for a specific “review request”, but the way I have it set up now it creates a review for a random review request when I select the “Offer a Review” button.
Here’s my reviews_controller:
class ReviewsController < ApplicationController
def create
@review = current_user.reviews.build(params[:review_request_id])
if @review.save
flash[:success] = "Review Created"
redirect_to review_path(@review)
else
flash[:error] = "Review Offer Sent"
redirect_to root_url
end
end
def show
@review_request = ReviewRequest.find(params[:id])
end
end
Here’s a section of the partial with the link_to the create action:
<span class="offer_review">
<%= link_to "Offer A Review", reviews_path(:review_request_id), :method => :post %>
</span>
I have
belongs_to :review_request
in the models/review.rb file and
belongs_to :user
in the review_request.rb file.
This is the index action in the review requests controller. The partial with the link is in the index view. May be part of the problem?
def index
@review_requests = ReviewRequest.paginate(page: params[:page])
end
There were actually two issues here. I solved the first one (the review wasn’t being created with the review_request_id) with:
in the review_requests_controller, and:
in the view.
I had actually solved this issue without realizing it before posting this question (and reverted back to the code above) because the redirect to the Review#show controller was wrong as well, and would show information for the wrong review_request even if the review was created correctly. (It did this because it took reviews/1 to be the review_request_id, not the review_id)
This is the code I got that to work right: