Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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!

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 %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top