Can't update a Review Scaffold in Ruby on Rails ? Error: NoMethodError in ReviewsController#update
Can't update a Review Scaffold in Ruby on Rails ? Error: NoMethodError in ReviewsController#update
我创建了一个嵌套在 Post 模型中的审查脚手架,但是当我尝试更新审查时,我收到此错误:
NoMethodError in ReviewsController#update
undefined method `review_url' for #<ReviewsController:0x007fe1420ea4e0>**
这是我的代码
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [ :edit, :update, :destroy, :upvote,:downvote]
before_action :set_post
before_action :authenticate_user!
respond_to :html
def new
@review = Review.new
respond_with(@review)
end
def edit
@review = Review.find(params[:id])
@post = Post.find(params[:post_id])
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.post_id = @post.id
@review.save
redirect_to post_path(@post)
end
def update
@review.update(review_params)
respond_with(@review)
end
def destroy
@review.destroy
respond_with(@review)
end
def upvote
@review.upvote_from current_user
redirect_to :back
end
def downvote
@review.downvote_from current_user
redirect_to :back
end
private
def set_review
@review = Review.find(params[:id])
end
def set_post
unless @post = Post.where(id: params[:post_id]).first
redirect_to posts_path, flash: {alert: "Post doesn't exists"}
end
end
def review_params
params.require(:review).permit(:comment)
end
end
post_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , only: [:edit,:update,:destroy,:new]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
@reviews = Review.where(post_id: @post.id)
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to root_path, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description)
end
end
reviews/_form.html.erb
<%= form_for([@post,@review]) do |f| %>
<% if @review.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% @review.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :comment %><br>
<%= f.text_area :comment , class: "form-control"%>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
以前有人遇到过这个问题吗?
这是我得到的屏幕截图图像:
routes.rb 文件
由于您使用的是嵌套路由,因此您需要 respond_with
这两种资源。 (参见 Nested resources documentation)。
您收到的错误是由于没有为 reviews#show
(reviews_url
) 定义路由,因此您需要创建嵌套在 posts
下的路由并添加ReviewsController
.
的操作
或者,您可以 respond_with(@post)
并让您的 reviews#update
操作将用户发送到 Posts#show
。
我创建了一个嵌套在 Post 模型中的审查脚手架,但是当我尝试更新审查时,我收到此错误:
NoMethodError in ReviewsController#update
undefined method `review_url' for #<ReviewsController:0x007fe1420ea4e0>**
这是我的代码
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [ :edit, :update, :destroy, :upvote,:downvote]
before_action :set_post
before_action :authenticate_user!
respond_to :html
def new
@review = Review.new
respond_with(@review)
end
def edit
@review = Review.find(params[:id])
@post = Post.find(params[:post_id])
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.post_id = @post.id
@review.save
redirect_to post_path(@post)
end
def update
@review.update(review_params)
respond_with(@review)
end
def destroy
@review.destroy
respond_with(@review)
end
def upvote
@review.upvote_from current_user
redirect_to :back
end
def downvote
@review.downvote_from current_user
redirect_to :back
end
private
def set_review
@review = Review.find(params[:id])
end
def set_post
unless @post = Post.where(id: params[:post_id]).first
redirect_to posts_path, flash: {alert: "Post doesn't exists"}
end
end
def review_params
params.require(:review).permit(:comment)
end
end
post_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , only: [:edit,:update,:destroy,:new]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
@reviews = Review.where(post_id: @post.id)
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to root_path, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description)
end
end
reviews/_form.html.erb
<%= form_for([@post,@review]) do |f| %>
<% if @review.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% @review.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :comment %><br>
<%= f.text_area :comment , class: "form-control"%>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
以前有人遇到过这个问题吗?
这是我得到的屏幕截图图像:
routes.rb 文件
由于您使用的是嵌套路由,因此您需要 respond_with
这两种资源。 (参见 Nested resources documentation)。
您收到的错误是由于没有为 reviews#show
(reviews_url
) 定义路由,因此您需要创建嵌套在 posts
下的路由并添加ReviewsController
.
或者,您可以 respond_with(@post)
并让您的 reviews#update
操作将用户发送到 Posts#show
。