如何正确删除这个?
这里严重没有添加某些内容。我的页面只是刷新,没有任何反应,它从未触及除索引之外的所有方法上挂出的任何调试器。
我的html:
<%- for image in @images %>
<%= image.attachment_file_name %>
<%-# link_to_delete image, :url => destroy_image_admin_wysiwyg_path(image.id) %>
<%= link_to 'delete', { :url => destroy_image_image_path(image.id) },
#:confirm => 'Are you sure?',
:post => true
%>
<br />
<% end %>
我的控制器
def destroy_image
debugger
@img = Image.find(params[:id])
@img.destroy
respond_to do |format|
format.html { redirect_to admin_image_rotator_path }
end
end
我的路线:
map.resources :images, :member => { :destroy_image => :post }
我的恶心黑客,一旦我找到更好的东西,我就会替换它
我将动作转移到我自己构建的一个更简单的控制器上。
将我的路线更改为:
admin.resources :wysiwygs, :member => { :destroy_image => :post }
更改了我的html:
<%= link_to 'delete', :controller => "wysiwygs", :action => "destroy_image" %>
但是当我单击链接时..它提出了.. show
操作? fffffffffuuuuuuu
我通过将我的动作移动到显示动作来报复,并在我的html中传递一个隐藏字段..
<%= link_to 'delete', :controller => "wysiwygs", :action => "destroy_image", :hidden_field => {:value => image.id} %>
def show
# this was previously in destroy_image
@img = Image.find(params[:hidden_field][:value])
@img.destroy
respond_to do |format|
format.html { redirect_to admin_image_rotator_path }
end
end
Something is seriously not adding up here.. My page just refreshes, nothing happens, it never touches any of my debuggers hanging out on all my methods except for index.
my html:
<%- for image in @images %>
<%= image.attachment_file_name %>
<%-# link_to_delete image, :url => destroy_image_admin_wysiwyg_path(image.id) %>
<%= link_to 'delete', { :url => destroy_image_image_path(image.id) },
#:confirm => 'Are you sure?',
:post => true
%>
<br />
<% end %>
my controller
def destroy_image
debugger
@img = Image.find(params[:id])
@img.destroy
respond_to do |format|
format.html { redirect_to admin_image_rotator_path }
end
end
My routes:
map.resources :images, :member => { :destroy_image => :post }
My disgusting hack that works that I will replace as soon as I find something better
I moved the action over to a simpler controller I built myself.
Changed my routes to :
admin.resources :wysiwygs, :member => { :destroy_image => :post }
Changed my html :
<%= link_to 'delete', :controller => "wysiwygs", :action => "destroy_image" %>
But when I clicked on the link..it brought up.. the show
action ?? fffffffffuuuuuuu
I retaliated by just moving my action to the show action, and passing a hidden field in my html..
<%= link_to 'delete', :controller => "wysiwygs", :action => "destroy_image", :hidden_field => {:value => image.id} %>
def show
# this was previously in destroy_image
@img = Image.find(params[:hidden_field][:value])
@img.destroy
respond_to do |format|
format.html { redirect_to admin_image_rotator_path }
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来你在这里走错了路。如果
before_filter
阻止了您的操作,找出原因。如果不需要过滤器,请使用skip_before_filter :filter_name
。不要使用
show
操作或 HTTP GET 进行删除。即使它有效,它也会让事情变得混乱。使用 DELETE 动词:map.resources :images, :member => { :destroy_image =>; :delete }
将其传递到链接助手中:
并使用
ImagesController#destroy_image
执行该操作。更好的是,考虑使用map.resources
免费为您提供的标准 RESTfulImagesController#destroy
。It seems you're going down the wrong path here. If a
before_filter
is blocking your action, figure out why. Useskip_before_filter :filter_name
if the filter is not needed.Don't use
show
actions or HTTP GET for deletes. Even if it works, it will confuse things down the road. Use a DELETE verb:map.resources :images, :member => { :destroy_image => :delete }
pass it in the link helper:
And use
ImagesController#destroy_image
to perform the action. Better yet, consider using the standard RESTfulImagesController#destroy
whichmap.resources
gives you for free.不确定首先出了什么问题,但在您的第二个工作解决方案中,我认为您应该按如下方式编写
link_to
:这至少会让您采取正确的操作。
根据您的路线,您必须将其设为
method => :post
或不。检查您的
rake paths
输出,它会向您显示可能的路线是什么,以及它们的名称,您可以将其用作方法(添加_path
或_url
位于末尾)。那么写这样的东西应该更容易:祝你好运!
Not sure what was wrong in the first place, but in your second, working solution, i think you should write your
link_to
as follows:That at least would send you to the correct action.
Depending on your routes, you will have to make this a
method => :post
or not.Check your
rake routes
output, it will show you what are the possible routes, and also what names they got, which in turn you can use as a method (add_path
or_url
at the end). Then it should be even easier to write something like:Good luck!
您正在执行 POST,但您的资源表示 :destroy_image 只能通过 GET 获得。尝试将您的路线更改为:
另外,请查看您的
link_to
。第二个参数采用 URL,而不是具有 :url 键的哈希值。正如其他地方提到的,根据您的 Rails 版本,您可能需要:method =>; :post
而不是:post =>正确。在 Rails 2.3.8 中,您可能需要这一行:
You're doing a POST but your resource says that :destroy_image is only available via GET. Try changing your route to:
Also, take a look at your
link_to
. The second parameter takes a URL, not a hash that has a :url key. As mentioned elsewhere, depending on your Rails version you may need:method => :post
instead of:post => true
. In Rails 2.3.8, you would want this line instead: