嗨,我想知道用户是否有办法更新他们已经写过的评论,我尝试使用 cancan 但遇到了一些问题,所以我想看看是否有更简单的方法。这是评论控制器中“新”方法的代码
def new
if logged_in?
@review = Review.new(:film_id => params[:id], :name =>
User.find(session[:user_id]).name)
session[:return_to] = nil
else
session[:return_to] = request.url
redirect_to login_path, alert: "You must be logged in to write a review"
end
end
和“创建”方法
def create
# use the class method 'new' with the parameter 'review', populated
# with values from a form
@review = Review.new(params[:review])
# attempt to save to the database, the new review instance variable
if @review.save
# use the class method 'find' with the id of the product of the
# saved review and assign this product object to the variable 'product'
film = Film.find(@review.film.id)
# redirect the reviewer to the show page of the product they reviewed,
# using the product variable, and send a notice indicating the review
# was successfully added
redirect_to film, notice: "Your review was successfully added"
else
# if the review could not be saved, return / render the new form
render action: "new"
end
end
如果他们已经为产品写过评论,我希望用户编辑他们的评论。而不是来自同一用户对同一产品的两次评论。