【问题标题】:NoMethodError in Reviews#new评论中的 NoMethodError#new
【发布时间】:2017-07-24 11:38:28
【问题描述】:

我正在尝试编写一个嵌套在餐厅内的新评论,但在评论#新操作中我一直没有收到任何方法错误。我一直在评论表上看到一条红线

<%= 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

rake 路线

                 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

【问题讨论】:

  • 发布您的rake routes 结果
  • 我已经添加了 rake 路线
  • 请发布完整的错误信息
  • 您的路线看起来如何?请添加代码

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


【解决方案1】:

未定义的方法“review_resturant_path” :0x007f8b3abe04e8>

你需要像下面这样切换@review@resturant的顺序

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

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

说明:

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

[@review, @resturant] #=&gt; 生成review_resturant_path 这是不正确

[@resturant, @review] #=&gt; 生成resturant_reviews_path,这是正确

或者,如果您觉得顺序混乱,您可以使用 route helper 生成哪些服务器具有相同的目的。所以表格如下所示

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

【讨论】:

    【解决方案2】:

    评论中的 NoMethodError#new

    这是错误的路径

    <%= 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 %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多