1

我創建了嵌套樁模型裏面,但是當我嘗試更新的評論我得到這個錯誤的評論支架:無法在Ruby on Rails中更新審閱腳手架?錯誤:NoMethodError在ReviewsController#更新

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

嘗試將表單更改爲'form_for(@review)',您也不需要在編輯操作中設置'@ review'和'@ post'變量,因爲它們都是用'before_action'設置的。最後,您可以運行'rake routes'並確保您有一條爲'reviews#update'定義的路線。 – dinjas

+0

我試過這樣做,但仍然得到相同的錯誤! – AHmed

+0

@AHmed,你能展示'rake routes'的結果嗎? – nsave

回答

1

由於您使用嵌套的路線,你需要respond_with這兩個資源。 (見Nested resources documentation)。

你得到的錯誤是由於沒有路由正在爲reviews#showreviews_url)定義的,所以你需要創建posts下嵌套在該路線和操作添加到您的ReviewsController

或者,您可以respond_with(@post)並讓您的reviews#update操作將用戶發送給Posts#show