Stefano
2011-Aug-22 11:48 UTC
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.. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Aug-22 12:28 UTC
Re: Routing error (route not found) but only for destroy action.
On Aug 22, 12:48 pm, Stefano <stefano....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> 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..-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud --
2011-Aug-22 12:28 UTC
Re: Routing error (route not found) but only for destroy action.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Aug-22 13:32 UTC
Re: Routing error (route not found) but only for destroy action.
On Aug 22, 1:28 pm, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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) >or polymorphic_path(item_to_delete) (which is what gets called if you do button_to(''delete'', item_to_delete, :method => ''delete'') ) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud --
2011-Aug-23 00:49 UTC
Re: Routing error (route not found) but only for destroy action.
Frederick Cheung wrote in post #1017860:> On Aug 22, 1:28pm, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> 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) >> > > or polymorphic_path(item_to_delete) (which is what gets called if you > do button_to(''delete'', item_to_delete, :method => ''delete'') ) >So what you are saying is that rails automatically does all that for you under the hood--if you specify a model object for the path in button_to()? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Aug-23 13:16 UTC
Re: Routing error (route not found) but only for destroy action.
On Aug 23, 1:49 am, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > > or polymorphic_path(item_to_delete) (which is what gets called if you > > do button_to(''delete'', item_to_delete, :method => ''delete'') ) > > So what you are saying is that rails automatically does all that for you > under the hood--if you specify a model object for the path in > button_to()?Yes. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.