“没有路线匹配”错误,但仅限于新的
我有一个表单,其中模型具有嵌套资源(使用 Carrierwave 上传的图像),并且在嵌套资源中我有一个允许删除图像的 link_to 。单击该链接将删除载波项目并删除图像。这一切对于编辑功能来说都很完美,但是当尝试进入新屏幕时,我收到以下错误:
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"}
I have a form where the model has a nested resource (an image uploaded with Carrierwave), and in the nested resource I have a link_to that allows the deletion of the image. Clicking the link removes the carrierwave item and deletes the image. This all works perfectly for the edit function, but when trying to go to the new screen I get the following error:
No route matches {:controller=>"listings", :action=>"remove_image"}
(ActionView::Template::Error)
Here is the link in question:
<%= link_to "Delete this image", remove_image_path(builder.object.id),
:confirm => "Are you sure you want to delete this image?",
:method => :delete %>
here is my routes entry:
match "/remove_image/:id" => "listings#remove_image", :as => "remove_image"
And here's the method from the 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
UPDATE: Rake routes
As requested, here are the routes related to the 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"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地这样做怎么样?
How about simply doing this?