Routing error (route not found) but only for destroy action.

Hi guys

I have many different models and I want to reuse the same code for the delete button. So in my helper i have:

def delete_button(item_to_delete)     button_to t('buttons.delete'), :action => "destroy", :controller => item_to_delete.class.to_s.downcase.pluralize end

Normally i work with paths a la post_path(item_to_destroy) but I cant find a way of making that work for different models except throwing in some stupid switch statement which makes the code REALLY ugly..

So the code above throws a route not found error. BUT: def delete_button(item_to_delete)     button_to t('buttons.delete'), :action => "new", :controller => item_to_delete.class.to_s.downcase.pluralize end

works just fine.. these are the things that really annoy me about ruby on rails.

Also.. the most ridiculous thing happens at the moment.. I use the asset pipeline and after rebooting all javascript code in my file users.js.coffee doesnt get executed in my browser anymore.. If i put it in example 'messages.js.coffee' it works just fine. EXACTLY the same syntax in both files.. I have NO idea why that happens. But frankly after wasting 1h on that I dont care. Just leave my code in the wrong file where it works..

Hi guys

I have many different models and I want to reuse the same code for the delete button. So in my helper i have:

def delete_button(item_to_delete) button_to t('buttons.delete'), :action => "destroy", :controller => item_to_delete.class.to_s.downcase.pluralize end

Normally i work with paths a la post_path(item_to_destroy) but I cant find a way of making that work for different models except throwing in some stupid switch statement which makes the code REALLY ugly..

So the code above throws a route not found error. BUT: def delete_button(item_to_delete) button_to t('buttons.delete'), :action => "new", :controller => item_to_delete.class.to_s.downcase.pluralize end

works just fine.. these are the things that really annoy me about ruby on rails.

You're not setting the method. REST dictates that the destroy action can only be accessed by the DELETE method. Also, given that the path you want to access is just the one for the post/comment/etc you don't need the path helper at all.

button_to 'Delete', item_to_delete, :method => 'DELETE'

should do the trick

Fred

Stefano wrote in post #1017840:

Hi guys

I have many different models and I want to reuse the same code for the delete button. So in my helper i have:

def delete_button(item_to_delete)     button_to t('buttons.delete'), :action => "destroy", :controller => item_to_delete.class.to_s.downcase.pluralize end

Normally i work with paths a la post_path(item_to_destroy) but I cant find a way of making that work for different models except throwing in some stupid switch statement which makes the code REALLY ugly..

In ruby, every method has to be called by an object, and that object is known as the 'receiver'. If you don't specify an object, then ruby uses self as the receiver. So when you call:

post_path(item_to_delete)

that is the same as:

self.post_path(item_to_delete)

The reason the object that is calling the method is called the reciever is because calling a method with an object in ruby is the same as sending a 'message' to the object. The message you send to the object is the name of the method. In ruby, you can write that like this:

self.send(:meth_name, args)

or

self.send(:post_path, item_to_delete)

In most cases, you can use a string instead of a symbol, so that can also be written as:

self.send("post_path", item_to_delete)

Once you have a way to execute a method when you have the name of the method as a string, you can get creative:

model = item_to_destroy.class.to_s.downcase

self.send("#{model}_path", item_to_delete)

So the code above throws a route not found error. BUT: def delete_button(item_to_delete)     button_to t('buttons.delete'), :action => "new", :controller => item_to_delete.class.to_s.downcase.pluralize end

works just fine.. these are the things that really annoy me about ruby on rails.

POST v. DELETE ???

Also.. the most ridiculous thing happens at the moment.. I use the asset pipeline and after rebooting all javascript code in my file users.js.coffee doesnt get executed in my browser anymore.. If i put it in example 'messages.js.coffee' it works just fine. EXACTLY the same syntax in both files.. I have NO idea why that happens. But frankly after wasting 1h on that I dont care. Just leave my code in the wrong file where it works..

Sorry, never used any coffescript.

or polymorphic_path(item_to_delete) (which is what gets called if you do button_to('delete', item_to_delete, :method => 'delete') )

Fred

Frederick Cheung wrote in post #1017860:

Yes.

Fred