Ruby Rails:ReviewsController#update中的NoMethodError

时间:2017-02-05 17:25:16

标签: ruby-on-rails ruby

我在编辑评论时一直收到以下错误,评论会更新,但是会在下面发出错误...任何想法我可能做错了什么? Ps:我是铁轨上的新手。

  

ReviewsController#update中的NoMethodError

我的控制器看起来像这样

class ReviewsController < ApplicationController
  before_action :authenticate_user! 
  before_action :set_listing
  before_action :set_review, only: [:edit, :update, :destroy, :upvote, :downvote]
  before_action :check_user, only: [:edit, :update, :destroy]

  # GET /reviews
  # GET /reviews.json


  def index
    @reviews = Review.all
  end

  # GET /reviews/1
  # GET /reviews/1.json
  def show
  end

  # GET /reviews/new
  def new
    @review = Review.new
  end

  # GET /reviews/1/edit
  def edit
  end

  #Added the def upvote and downvote
  def upvote
    @listing = Listing.find(params[:listing_id])
    @review = Review.find(params[:id])
    @review.upvote_from current_user
    redirect_to :back
  end

  def downvote
    @listing = Listing.find(params[:listing_id])
    @review = Review.find(params[:id])
    @review.downvote_from current_user
    redirect_to :back
  end


  # POST /reviews
  # POST /reviews.json
  def create
    @review = Review.new(review_params)
    @review.user_id = current_user.id
    @review.listing_id = @listing.id

    respond_to do |format|
        if @review.save
          format.html { redirect_to @listing, notice: 'Your review was successfully posted.' }
          format.json { render :show, status: :created, location: @review }
        else
          format.html { render :new }
          format.json { render json: @review.errors, status: :unprocessable_entity }
        end
      end
    end

  # PATCH/PUT /reviews/1
  # PATCH/PUT /reviews/1.json
  def update
    respond_to do |format|
      if @review.update(review_params)
        format.html { redirect_to @review, notice: 'Your Review was successfully updated.' }
        format.json { render :show, status: :ok, location: @review }
      else
        format.html { render :edit }
        format.json { render json: @review.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /reviews/1
  # DELETE /reviews/1.json
  def destroy
    @review.destroy
    respond_to do |format|
      format.html { redirect_to listing_path(@listing), notice: 'Your Review was successfully destroyed :(.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_review
      @review = Review.find(params[:id])
    end

    #Check user

     def check_user
      unless (@review.user == current_user) || (current_user.admin?)
        redirect_to root_url, alert: "Sorry, this review belongs to someone else, you can only edit reviews you have posted."
      end
    end

    #set listing

    def set_listing
      @listing = Listing.find(params[:listing_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def review_params
      params.require(:review).permit(:rating, :comment, :upvote, :downvote)
    end
   
end

我的路线文件如下

Rails.application.routes.draw do

  devise_for :users

  resources :listings do
    resources :reviews, except: [:show, :index, :upvote, :downvote] do
      resources :user do
        put "upvote", to: "reviews#upvote"
        put "downvote", to: "reviews#downvote"
      end
    end
  end

  
  get 'pages/about'

  get 'pages/how'

  get 'pages/faqs'

  get 'pages/contact'

  get 'pages/privacy'

  get 'pages/tos'

  get 'pages/guidelines'

  root 'listings#index'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我的观看页面如下

<div class="row">
  <div class="col-md-3">
    <%= image_tag @listing.image_url (:medium) %>

<p>
  <h3><%= @listing.name %></h3>
</p>
<div class="star-rating" data-score= <%= @avg_rating  %> ></div>
<p class="small"><%= "#{@reviews.length} reviews" %></p>

<address>
  <strong>Address:</strong>
  <%= @listing.address %><br>
  <strong>Phone:</strong>
  <%= @listing.phone %><br>
  <strong>Email:</strong>
  <%= mail_to @listing.email %> <br>
  <strong>Website:</strong>
  <%= link_to @listing.website, @listing %>
</address>
<hr>
<p>
  <strong>About:</strong>
  <p><%= h(@listing.description).gsub(/\n/, '<br/>').html_safe %></p>
</p>

  </div>
  <div class="col-md-9">
    <%= link_to "Haiya, Post a Review ", new_listing_review_path(@listing), class: "btn btn-info" %> <br><br>

      <table class="table">
        <thead>
            <tr>
              <th class="col-md-3"></th>
              <th class="col-md-9"></th>
            </tr>
          </thead>
           
          <tbody>
             <% if @reviews.blank? %>
             <tr>
                <p>No reviews yet. Be the first to write one!</p>
          <% else %>
            <% @reviews.each do |review| %>
              
                <td>
                  <h4>
                    <%= "#{review.user.first_name.capitalize} #{review.user.last_name.capitalize[0]}." %>
                  </h4>
                  <p class = "small"><!--<%= review.created_at.strftime("%B, %d, %Y") %>--> 
                  <%= time_ago_in_words(review.created_at) %> ago </p>
                </td>
                <td>
                  <div class="star-rating" data-score= <%= review.rating %> ></div>
                  <p><%= h(review.comment).gsub(/\n/, '<br/>').html_safe %></p>
              
                  
                  <div class ="text-right"> 
                    <p class ="small"><%= link_to listing_review_user_upvote_path(:listing, :review, :user), method: :put do %>
                          Poa
                          <%= review.get_upvotes.size %>
                        <% end %> |

                        <%= link_to listing_review_user_downvote_path(:listing, :review, :user), method: :put do %>
                          Zee
                          <%= review.get_upvotes.size %>
                        <% end %></p> 


                     <!-- insert voting links here-->
                      </div>

                    <% if user_signed_in? %>
                  <% if (review.user == current_user) || (current_user.admin?) %>

                      <%= link_to "Edit", edit_listing_review_path(@listing, review), class: "text-left"  %>
                      <%= link_to "Delete", listing_review_path(@listing, review), method: :delete, class: "text-left"  %>
 
                    
                  <% end %>
                <% end %>

                </td>
              </tr>
            <% end %>
            <% end %>
          </tbody>

        </table>

  </div>
</div>

<% if user_signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_listing_path(@listing), class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', listings_path, class: "btn btn-link" %>

<!--The script for star ratings. -->

<script>
  $('.star-rating').raty({
    path: 'https://s3.eu-west-2.amazonaws.com/bebuwaphotos/uploads/stars',
    readOnly: true,
    score: function() {
      return $(this).attr('data-score');
    }
  });
</script>

这是错误

enter image description here

当我删除:在routes.rb文件上显示

时出现错误

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在@review中的update action重定向到ReviewsController,但是您在routes.rb中将其免除,将其取消,一切都应该没问题。只需从except数组中删除:show