当前位置:  开发笔记 > 后端 > 正文

没有路由匹配嵌套资源的[DELETE]

如何解决《没有路由匹配嵌套资源的[DELETE]》经验,为你挑选了1个好方法。

我有一个嵌套的模型设置,用户有多个制造商,制造商有多条线.当我尝试从行索引中删除该行的实例时,我得到一条错误的路由消息.我不确定我做错了什么,但它可能很简单,希望有人可以在这里省点我的心痛.我已经阅读了hartl的东西,我对嵌套资源和路由仍然非常新.

===模型===

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :manufacturers
end

======

class Manufacturer < ActiveRecord::Base
  belongs_to :user
  has_many :lines
end

======

class Line < ActiveRecord::Base
  belongs_to :manufacturer
end

======

lines_controller.rb

class LinesController < ApplicationController
  before_action :set_line, only: [:show, :edit, :update, :destroy]
  before_filter :load_manufacturer

  # GET /lines
  # GET /lines.json
  def index
    @lines = Line.all
  end

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

  # GET /lines/new
  def new
    @manufacturer = Manufacturer.find(params[:manufacturer_id])
    @line = @manufacturer.lines.build
  end

  # GET /lines/1/edit
  def edit
  end

  # POST /lines
  # POST /lines.json
  def create
    @line = @manufacturer.lines.build(line_params)

    respond_to do |format|
      if @line.save
        format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully created.' }
        format.json { render action: 'show', status: :created, location: @line }
      else
        format.html { render action: 'new' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /lines/1
  # PATCH/PUT /lines/1.json
  def update
    respond_to do |format|
      if @line.update(line_params)
        format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /lines/1
  # DELETE /lines/1.json
  def destroy
    @manufacturer.line.destroy
    respond_to do |format|
      format.html { redirect_to lines_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_params
      params.require(:line).permit(:name, :manufacturer_id)
    end

    def load_manufacturer
      @manufacturer = Manufacturer.find(params[:manufacturer_id])
    end
end

=====

                  Prefix Verb   URI Pattern                                              Controller#Action
      manufacturer_lines GET    /manufacturers/:manufacturer_id/lines(.:format)          lines#index
                         POST   /manufacturers/:manufacturer_id/lines(.:format)          lines#create
   new_manufacturer_line GET    /manufacturers/:manufacturer_id/lines/new(.:format)      lines#new
  edit_manufacturer_line GET    /manufacturers/:manufacturer_id/lines/:id/edit(.:format) lines#edit
       manufacturer_line GET    /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#show
                         PATCH  /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#update
                         PUT    /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#update
                         DELETE /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#destroy
           manufacturers GET    /manufacturers(.:format)                                 manufacturers#index
                         POST   /manufacturers(.:format)                                 manufacturers#create
        new_manufacturer GET    /manufacturers/new(.:format)                             manufacturers#new
       edit_manufacturer GET    /manufacturers/:id/edit(.:format)                        manufacturers#edit
            manufacturer GET    /manufacturers/:id(.:format)                             manufacturers#show
                         PATCH  /manufacturers/:id(.:format)                             manufacturers#update
                         PUT    /manufacturers/:id(.:format)                             manufacturers#update
                         DELETE /manufacturers/:id(.:format)                             manufacturers#destroy
        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
           user_password POST   /users/password(.:format)                                devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                            devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                           devise/passwords#edit
                         PATCH  /users/password(.:format)                                devise/passwords#update
                         PUT    /users/password(.:format)                                devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                                  devise/registrations#cancel
       user_registration POST   /users(.:format)                                         devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                                 devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                                    devise/registrations#edit
                         PATCH  /users(.:format)                                         devise/registrations#update
                         PUT    /users(.:format)                                         devise/registrations#update
                         DELETE /users(.:format)                                         devise/registrations#destroy
                    root GET    /                                                        pages#home
                   about GET    /about(.:format)                                         pages#about
                         GET    /users/:id(.:format)                                     users#show
               all_users GET    /users(.:format)                                         users#index
          public_profile GET    /users/:name(.:format)                                   users#show

======

应用程序/视图/线/ index.html.erb

Listing lines

<% @lines.each do |line| %> <% end %>
Name Manufacturer
<%= line.name %> <%= line.manufacturer_id %> <%= link_to 'Show', manufacturer_lines_path(line.manufacturer, line) %> <%= link_to 'Edit', edit_manufacturer_line_path(line.manufacturer, line) %> <%= link_to 'Destroy', manufacturer_lines_path, method: :delete, data: { confirm: 'Are you sure?' } %>

Ryan Bigg.. 13

这是试图使用错误的路由助手:

<%= link_to 'Destroy', manufacturer_lines_path, method: :delete, data: { confirm: 'Are you sure?' } %>

它应该是:

<%= link_to 'Destroy', manufacturer_line_path(line.manufacturer, line), method: :delete, data: { confirm: 'Are you sure?' } %>

什么manufacturer_lines_path是做使用路由帮手产生,如路径是/manufacturers/1/lines,这是完全有效的.什么是合法的是,这个环节作出DELETE该路由请求,如果你在你的样子rake routes输出,你会看到,你不必为这个路线.你可能只有一个GET和另一个POST,但不是DELETE.

因此,解决方案是使用正确的路径助手,manufacturer_line_path它接受一个Manufacturer对象和一个Line对象,并从中生成一个路径,如/manufacturers/1/lines/2.再次,查看输出rake routes,您将看到为此路径定义了几个路由.A GET,a PUT,a PATCH和a DELETE.

这最后的路线是怎么回事进行匹配,当你做出一个DELETE /manufacturers/1/lines/2请求,你的应用程序的路由器将路由到LinesControllerdestroy动作,传递manufacturer_id=1id=2在参数.



1> Ryan Bigg..:

这是试图使用错误的路由助手:

<%= link_to 'Destroy', manufacturer_lines_path, method: :delete, data: { confirm: 'Are you sure?' } %>

它应该是:

<%= link_to 'Destroy', manufacturer_line_path(line.manufacturer, line), method: :delete, data: { confirm: 'Are you sure?' } %>

什么manufacturer_lines_path是做使用路由帮手产生,如路径是/manufacturers/1/lines,这是完全有效的.什么是合法的是,这个环节作出DELETE该路由请求,如果你在你的样子rake routes输出,你会看到,你不必为这个路线.你可能只有一个GET和另一个POST,但不是DELETE.

因此,解决方案是使用正确的路径助手,manufacturer_line_path它接受一个Manufacturer对象和一个Line对象,并从中生成一个路径,如/manufacturers/1/lines/2.再次,查看输出rake routes,您将看到为此路径定义了几个路由.A GET,a PUT,a PATCH和a DELETE.

这最后的路线是怎么回事进行匹配,当你做出一个DELETE /manufacturers/1/lines/2请求,你的应用程序的路由器将路由到LinesControllerdestroy动作,传递manufacturer_id=1id=2在参数.

推荐阅读
围脖上的博博_771
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有