如何正确删除这个?

发布于 2024-10-06 04:33:02 字数 1680 浏览 0 评论 0原文

这里严重没有添加某些内容。我的页面只是刷新,没有任何反应,它从未触及除索引之外的所有方法上挂出的任何调试器。

我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘和柠 2024-10-13 04:33:02

看来你在这里走错了路。如果 before_filter 阻止了您的操作,找出原因。如果不需要过滤器,请使用 skip_before_filter :filter_name

不要使用 show 操作或 HTTP GET 进行删除。即使它有效,它也会让事情变得混乱。使用 DELETE 动词:

map.resources :images, :member => { :destroy_image =>; :delete }

将其传递到链接助手中:

<%= link_to "delete", destroy_image_image_path(image), :method => :delete %>

并使用 ImagesController#destroy_image 执行该操作。更好的是,考虑使用 map.resources 免费为您提供的标准 RESTful ImagesController#destroy

It seems you're going down the wrong path here. If a before_filter is blocking your action, figure out why. Use skip_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:

<%= link_to "delete", destroy_image_image_path(image), :method => :delete %>

And use ImagesController#destroy_image to perform the action. Better yet, consider using the standard RESTful ImagesController#destroy which map.resources gives you for free.

筱武穆 2024-10-13 04:33:02

不确定首先出了什么问题,但在您的第二个工作解决方案中,我认为您应该按如下方式编写 link_to

link_to 'delete', :controller => "wysiwygs", :action => "destroy_image", :id => image.id    

这至少会让您采取正确的操作。
根据您的路线,您必须将其设为 method => :post 或不。
检查您的rake paths输出,它会向您显示可能的路线是什么,以及它们的名称,您可以将其用作方法(添加_path_url 位于末尾)。那么写这样的东西应该更容易:

link_to 'delete', wysiwygs_destroy_image_path(image)

祝你好运!

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:

link_to 'delete', :controller => "wysiwygs", :action => "destroy_image", :id => image.id    

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:

link_to 'delete', wysiwygs_destroy_image_path(image)

Good luck!

鹿港小镇 2024-10-13 04:33:02

您正在执行 POST,但您的资源表示 :destroy_image 只能通过 GET 获得。尝试将您的路线更改为:

map.resources :images, :member => { :destroy_image => :post }

另外,请查看您的 link_to。第二个参数采用 URL,而不是具有 :url 键的哈希值。正如其他地方提到的,根据您的 Rails 版本,您可能需要 :method =>; :post 而不是 :post =>正确。在 Rails 2.3.8 中,您可能需要这一行:

<%= link_to 'delete', destroy_image_image_path(image), :method => :post %>

You're doing a POST but your resource says that :destroy_image is only available via GET. Try changing your route to:

map.resources :images, :member => { :destroy_image => :post }

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:

<%= link_to 'delete', destroy_image_image_path(image), :method => :post %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文