无法在Ruby on Rails中更新Review Scaffold?错误:ReviewsController#update中的NoMethodError

时间:2015-10-19 21:59:43

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我创建了一个嵌套在Post Model中的评论脚手架但是当我尝试更新Review时出现此错误:

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

评论/ _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 %>

之前有没有人遇到过这个问题?

这是我获得的屏幕截图: NoMethodError in ReviewsController#update

routes.rb文件

Rake Routes

1 个答案:

答案 0 :(得分:1)

由于您使用的是嵌套路由,因此需要respond_with这两种资源。 (见Nested resources documentation)。

您获得的错误是由于没有为reviews#showreviews_url)定义路由,因此您需要创建嵌套在posts下的路线并添加操作到你的ReviewsController

或者,您可以respond_with(@post)并让reviews#update操作将用户发送到Posts#show