评论#new中的NoMethodError

时间:2017-07-24 11:38:28

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5

我正在尝试编写一个嵌套在resturants中的新评论,但我在评论中不断出现方法错误#new action。我一直在评论表格上看到一条红线

<%= form_with(model: [@review, @resturant] , local: true) do |form| %>

完整错误消息:

NoMethodError in Reviews#new
Showing /Users/AHmed/Desktop/burgerland-ar/app/views/reviews/_form.html.erb
     

第1行引出:

undefined method `review_resturant_path' for #<#<Class:0x007f8b3abe3170>:0x007f8b3abe04e8>
Did you mean?  resturant_reviews_path
               resturant_review_path
               new_resturant_path

新的rails 5语法是否已更改或错误是否与审核控制器有关?这是我的代码:

评论/ _form.html.erb

<%= form_with(model: [@review, @resturant] , local: true) do |form| %>
  <% 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="field">
    <%= form.label :rating %>
    <%= form.number_field :rating, id: :review_rating %>
  </div>

  <div class="field">
    <%= form.label :comment %>
    <%= form.text_area :comment, id: :review_comment %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

/reviews_controller.rb

class ReviewsController < ApplicationController
  before_action :set_review, only: [:edit, :update, :destroy]
   before_action :set_resturant
  before_action :authenticate_user!



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

  # GET /reviews/1/edit
  def edit
  end

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

    respond_to do |format|
      if @review.save
        format.html { redirect_to root_path, notice: 'Review was successfully created.' }
        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: '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 reviews_url, notice: '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

    def set_resturant
      @resturant = Resturant.find(params[:resturant_id])
    end

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

佣金路线

                 Prefix Verb   URI Pattern                                          Controller#Action
        new_user_session GET    /users/sign_in(.:format)                             devise/sessions#new
            user_session POST   /users/sign_in(.:format)                             devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                            devise/sessions#destroy
       new_user_password GET    /users/password/new(.:format)                        devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                       devise/passwords#edit
           user_password PATCH  /users/password(.:format)                            devise/passwords#update
                         PUT    /users/password(.:format)                            devise/passwords#update
                         POST   /users/password(.:format)                            devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)                              devise/registrations#cancel
   new_user_registration GET    /users/sign_up(.:format)                             devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                                devise/registrations#edit
       user_registration PATCH  /users(.:format)                                     devise/registrations#update
                         PUT    /users(.:format)                                     devise/registrations#update
                         DELETE /users(.:format)                                     devise/registrations#destroy
                         POST   /users(.:format)                                     devise/registrations#create
       resturant_reviews POST   /resturants/:resturant_id/reviews(.:format)          reviews#create
    new_resturant_review GET    /resturants/:resturant_id/reviews/new(.:format)      reviews#new
   edit_resturant_review GET    /resturants/:resturant_id/reviews/:id/edit(.:format) reviews#edit
        resturant_review PATCH  /resturants/:resturant_id/reviews/:id(.:format)      reviews#update
                         PUT    /resturants/:resturant_id/reviews/:id(.:format)      reviews#update
                         DELETE /resturants/:resturant_id/reviews/:id(.:format)      reviews#destroy
              resturants GET    /resturants(.:format)                                resturants#index
                         POST   /resturants(.:format)                                resturants#create
           new_resturant GET    /resturants/new(.:format)                            resturants#new
          edit_resturant GET    /resturants/:id/edit(.:format)                       resturants#edit
               resturant GET    /resturants/:id(.:format)                            resturants#show
                         PATCH  /resturants/:id(.:format)                            resturants#update
                         PUT    /resturants/:id(.:format)                            resturants#update
                         DELETE /resturants/:id(.:format)                            resturants#destroy
                    root GET    /                                                    resturants#index
             pages_about GET    /pages/about(.:format)                               pages#about
              pages_help GET    /pages/help(.:format)                                pages#help

的routes.rb

Rails.application.routes.draw do

  devise_for :users
  resources :resturants do 
  resources :reviews , except: [:index,:show]
end
  root 'resturants#index'
  get 'pages/about'

  get 'pages/help'

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

2 个答案:

答案 0 :(得分:2)

  

未定义的方法`review_resturant_path'for   &LT;#&LT;班级:0x007f8b3abe3170&GT;:0x007f8b3abe04e8&GT;

您需要切换@review@resturant的顺序,如下所示

<%= form_with(model: [@resturant, @review] , local: true) do |form| %>

所以它会生成正确的路线帮助

<强>解释

在为嵌套资源构建表单时,请确保将资源(模型实例)置于正确的顺序中。是的,订单很重要!

[@review, @resturant] #=>生成review_resturant_path 不正确

[@resturant, @review] #=>生成resturant_reviews_path 正确

或者,如果您认为订单令人困惑,您可以使用生成的路径助手来实现相同的目的。所以表单如下所示

<%= form_with url: resturant_reviews_path(@resturant) do |form| %>

答案 1 :(得分:1)

  

评论#new中的NoMethodError

错误路径

<%= form_with(model: [@review, @resturant] , local: true) do |form| %>

查看您的路线resturant_reviews_path哪一个正确所以

你应该这样做:

<%= form_with(model: [@resturant, @review] , local: true) do |form| %>
  <% 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="field">
    <%= form.label :rating %>
    <%= form.number_field :rating, id: :review_rating %>
  </div>

  <div class="field">
    <%= form.label :comment %>
    <%= form.text_area :comment, id: :review_comment %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>