DELETE方法的路由不正确

时间:2014-05-05 15:34:29

标签: ruby-on-rails routes restful-url

出于某种原因,slide_path为我提供了/slides/1.1 DELETE方法,而不是/albums/1/slides/1。提前感谢任何有关它发生的建议或/以及如何解决它。

我的规范失败了:

 1) Slide Pages Delete slide
     Failure/Error: expect{page.find('.btn.btn-mini.btn-danger').click}.to change(Slide, :count).by(-1)
     ActionController::RoutingError:
       No route matches [DELETE] "/slides/1.1"
     # ./spec/features/slides_spec.rb:78:in `block (3 levels) in <top (required)>'
     # ./spec/features/slides_spec.rb:78:in `block (2 levels) in <top (required)>'

规格:

 scenario "Delete slide" do
    visit album_slides_url slide.album
    expect{page.find('.btn.btn-mini.btn-danger').click}.to change(Slide, :count).by(-1)
  end

查看:

    <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
    slide_path(slide.album.id, slide.id),
    :method => :delete,
    :class => 'btn btn-mini btn-danger',
    :remote => true %>

佣金路线:

  slide GET      /slides/:id(.:format)                                             slides#show
        PATCH    /slides/:id(.:format)                                             slides#update
        PUT      /slides/:id(.:format)                                             slides#update
        DELETE   /albums/:album_id/slides/:ids(.:format)                           slides#destroy # this is the one I expect to get
        PATCH    /albums/:album_id/slides/:ids/dest_album/:dest_album_id(.:format) slides#move
        PUT      /albums/:album_id/slides/:ids/dest_album/:dest_album_id(.:format) slides#move

的routes.rb

  resources :albums, except: :show do
    resources :slides, shallow: true, except: [:destroy, :patch, :put]    
    match 'slides/:ids', to: 'slides#destroy', via: :delete
    match 'slides/:ids/dest_album/:dest_album_id', to: 'slides#move', via: :patch
    match 'slides/:ids/dest_album/:dest_album_id', to: 'slides#move', via: :put
  end

1 个答案:

答案 0 :(得分:0)

根据您定义的路由,DELETE路由不是命名路由,因此当您在链接中说slide_path(slide.album.id, slide.id)时,rails路由会自动尝试将其与前缀为{的列出路由匹配{1}}以及slide请求。

要将删除幻灯片链接正确路由到DELETE路由,您需要更新路由,如下所示:

DELETE

这将创建resources :albums, except: :show do resources :slides, shallow: true, except: [:destroy, :patch, :put] match 'slides/:ids', to: 'slides#destroy', via: :delete, as: :slide ## Note as option match 'slides/:ids/dest_album/:dest_album_id', to: 'slides#move', via: :patch match 'slides/:ids/dest_album/:dest_album_id', to: 'slides#move', via: :put end 命名路由,如下所示:

DELETE

现在,更新“删除幻灯片”链接以映射到此路线:

album_slide DELETE /albums/:album_id/slides/:ids(.:format)  slides#destroy

<强>更新

您的路线不是命名路线。根据您当前定义的路线<%= link_to t('.destroy', :default => t("helpers.links.destroy")), album_slide_path(slide.album.id, slide.id), :method => :delete, :class => 'btn btn-mini btn-danger', :remote => true %> 仅适用于以下路线:

slide_path

是使用 slide GET /slides/:id(.:format) slides#show PATCH /slides/:id(.:format) slides#update PUT /slides/:id(.:format)

创建的

因此,当您使用两个参数调用resources :slides, shallow: true, except: [:destroy, :patch, :put]时,rails路由器尝试将其转换为路径

slide_path(slide.album.id, slide.id)

因此您收到了带路径的请求

/slides/:id(.:format) 

其中

slides/1.1 ^ ^ a b =&gt;是指slide_path路由的a动态段,:id的值设置为

slide.album.id =&gt;指的是slide_path路由的b部分,(.:format)的值设置为它。格式是指扩展名(.html,.json等),因此您在slide.id被视为.1的路径中收到slide.id