“没有路由匹配”错误,但仅限于新的

时间:2011-09-12 21:07:58

标签: ruby-on-rails controller

我有一个表单,其中模型具有嵌套资源(使用Carrierwave上传的图像),并且在嵌套资源中我有一个允许删除图像的link_to。单击该链接将删除carrierwave项并删除该图像。这一切都完美适用于编辑功能,但在尝试转到新屏幕时,我收到以下错误:

No route matches {:controller=>"listings", :action=>"remove_image"} 
(ActionView::Template::Error)

以下是相关链接:

    <%= link_to "Delete this image", remove_image_path(builder.object.id), 
        :confirm => "Are you sure you want to delete this image?", 
        :method => :delete %>

这是我的路线条目:

match "/remove_image/:id" => "listings#remove_image", :as => "remove_image"

这是来自ListingsController的方法:

def remove_image
  @image = SecondaryImage.find(params[:id])
  @listing = Listing.find(@image.listing_id)
  @image.remove_image
  @image.destroy
  respond_to do |format|
  format.html { redirect_to edit_listing_path(@listing), 
                notice: "Successfully deleted secondary image" }
  end 
end

更新:佣金路线 根据要求,以下是与ListingsController相关的路由

   listing_secondary_images GET    /listings/:listing_id/secondary_images(.:format)          {:action=>"index", :controller=>"secondary_images"}
                            POST   /listings/:listing_id/secondary_images(.:format)          {:action=>"create", :controller=>"secondary_images"}
new_listing_secondary_image GET    /listings/:listing_id/secondary_images/new(.:format)      {:action=>"new", :controller=>"secondary_images"}
edit_listing_secondary_image GET    /listings/:listing_id/secondary_images/:id/edit(.:format) {:action=>"edit", :controller=>"secondary_images"}
 listing_secondary_image GET    /listings/:listing_id/secondary_images/:id(.:format)      {:action=>"show", :controller=>"secondary_images"}
                         PUT    /listings/:listing_id/secondary_images/:id(.:format)      {:action=>"update", :controller=>"secondary_images"}
                         DELETE /listings/:listing_id/secondary_images/:id(.:format)      {:action=>"destroy", :controller=>"secondary_images"}
                listings GET    /listings(.:format)                                       {:action=>"index", :controller=>"listings"}
                         POST   /listings(.:format)                                       {:action=>"create", :controller=>"listings"}
             new_listing GET    /listings/new(.:format)                                   {:action=>"new", :controller=>"listings"}
            edit_listing GET    /listings/:id/edit(.:format)                              {:action=>"edit", :controller=>"listings"}
                 listing GET    /listings/:id(.:format)                                   {:action=>"show", :controller=>"listings"}
                         PUT    /listings/:id(.:format)                                   {:action=>"update", :controller=>"listings"}
                         DELETE /listings/:id(.:format)                                   {:action=>"destroy", :controller=>"listings"}
                features GET    /features(.:format)                                       {:action=>"index", :controller=>"features"}
                         POST   /features(.:format)                                       {:action=>"create", :controller=>"features"}
             new_feature GET    /features/new(.:format)                                   {:action=>"new", :controller=>"features"}
            edit_feature GET    /features/:id/edit(.:format)                              {:action=>"edit", :controller=>"features"}
                 feature GET    /features/:id(.:format)                                   {:action=>"show", :controller=>"features"}
                         PUT    /features/:id(.:format)                                   {:action=>"update", :controller=>"features"}
                         DELETE /features/:id(.:format)                                   {:action=>"destroy", :controller=>"features"}
                  orders GET    /orders(.:format)                                         {:action=>"index", :controller=>"orders"}
                         POST   /orders(.:format)                                         {:action=>"create", :controller=>"orders"}
               new_order GET    /orders/new(.:format)                                     {:action=>"new", :controller=>"orders"}
              edit_order GET    /orders/:id/edit(.:format)                                {:action=>"edit", :controller=>"orders"}
                   order GET    /orders/:id(.:format)                                     {:action=>"show", :controller=>"orders"}
                         PUT    /orders/:id(.:format)                                     {:action=>"update", :controller=>"orders"}
                         DELETE /orders/:id(.:format)                                     {:action=>"destroy", :controller=>"orders"}
         preview_listing GET    /preview/:id(.:format)                                    {:controller=>"listings", :action=>"preview"}
         approve_listing        /approve/:id(.:format)                                    {:controller=>"listings", :action=>"approve"}
            remove_image        /remove_image/:id(.:format)                               {:controller=>"listings", :action=>"remove_image"}

1 个答案:

答案 0 :(得分:0)

如何简单地这样做?

<%= link_to "Delete this image", "/remove_image/#{builder.object.id}", 
    :confirm => "Are you sure you want to delete this image?", 
    :method => :delete %>