admin-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org
2009-Aug-15 12:52 UTC
ActionController::UrlWriter bug -- need help URGENTLY, thanks!
So i''ve been banging my head against this for the better part of the last 5 hours and I have concluded that in my sleep deprived state I''m missing something really obvious and should just ask for help. I need to be able to access named route helpers (XXX_path and XXX_URL) form a class method in an ActiveRecord model. I tried a couple of approaches none of which worked: 1) include ActionController::UrlWriter in the model class -- this doesn''t work because the included methods are instance methods and thus are not available in the class methods 2) According to the rails api you can access the helpers by using ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ classes/ActionController/UrlWriter.html). However that just doesn''t work for me, I get the following error NoMethodError: undefined method `xxx_path'' for ActionController::UrlWriter:Module If anyone has ANY ideas PLEASE let me know.
Frederick Cheung
2009-Aug-15 13:14 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
On Aug 15, 1:52 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote:> So i''ve been banging my head against this for the better part of the > last 5 hours and I have concluded that in my sleep deprived state I''m > missing something really obvious and should just ask for help. > > I need to be able to access named route helpers (XXX_path and XXX_URL) > form a class method in an ActiveRecord model. I tried a couple of > approaches none of which worked: > > 1) include ActionController::UrlWriter in the model class -- this > doesn''t work because the included methods are instance methods and > thus are not available in the class methods > > 2) According to the rails api you can access the helpers by using > ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ > classes/ActionController/UrlWriter.html). However that just doesn''t > work for me, I get the following error > > NoMethodError: undefined method `xxx_path'' for > ActionController::UrlWriter:Module > > If anyone has ANY ideas PLEASE let me know.The helpers are defined in an anonymous module hidden inside the routing stuff. You can ask rails to include that module in a class for you, eg class Foo include ActionController::UrlWriter def generate parent_categories_url :host => ''example.com'' end end ActionController::Routing::Routes.named_routes.install(Foo) Foo.new.generate #=> "http://example.com/admin/parent_categories" Fred
admin-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org
2009-Aug-15 13:22 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
This only works for instance methods, my problem is that I need to be able to access xxxx_path from a class method... meaning: class Foo include ActionController::UrlWriter def self.generate login_path :host => ''example.com'' end end ActionController::Routing::Routes.named_routes.install(Foo) Foo.generate #=> NameError: undefined local variable or method `login_path'' for Foo:Class from (irb):4:in `generate'' from (irb):9 On Aug 15, 9:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 15, 1:52 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > > > So i''ve been banging my head against this for the better part of the > > last 5 hours and I have concluded that in my sleep deprived state I''m > > missing something really obvious and should just ask for help. > > > I need to be able to access named route helpers (XXX_path and XXX_URL) > > form a class method in an ActiveRecord model. I tried a couple of > > approaches none of which worked: > > > 1) include ActionController::UrlWriter in the model class -- this > > doesn''t work because the included methods are instance methods and > > thus are not available in the class methods > > > 2) According to the rails api you can access the helpers by using > > ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ > > classes/ActionController/UrlWriter.html). However that just doesn''t > > work for me, I get the following error > > > NoMethodError: undefined method `xxx_path'' for > > ActionController::UrlWriter:Module > > > If anyone has ANY ideas PLEASE let me know. > > The helpers are defined in an anonymous module hidden inside the > routing stuff. You can ask rails to include that module in a class for > you, eg > > class Foo > include ActionController::UrlWriter > def generate > parent_categories_url :host => ''example.com'' > end > end > ActionController::Routing::Routes.named_routes.install(Foo) > > Foo.new.generate #=> "http://example.com/admin/parent_categories" > > Fred
Frederick Cheung
2009-Aug-15 13:32 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
On Aug 15, 2:22 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote:> This only works for instance methods, my problem is that I need to be > able to access xxxx_path from a class method... meaning:Well there''s nothing stopping you bundling up your url generation stuff into its own class and then do UrlGenerator.new.generate (I''d even reccomend it seing as install the routes will pollute your model''s name space with loads of methods. A small experiment shows that ActionController::Routing::Routes.named_routes.install isn''t actually needed - UrlWriter''s included method seems to call that for you Fred> > class Foo > include ActionController::UrlWriter > def self.generate > login_path :host => ''example.com'' > end > end > ActionController::Routing::Routes.named_routes.install(Foo) > > Foo.generate > #=> NameError: undefined local variable or method `login_path'' for > Foo:Class > from (irb):4:in `generate'' > from (irb):9 > > On Aug 15, 9:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Aug 15, 1:52 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > > So i''ve been banging my head against this for the better part of the > > > last 5 hours and I have concluded that in my sleep deprived state I''m > > > missing something really obvious and should just ask for help. > > > > I need to be able to access named route helpers (XXX_path and XXX_URL) > > > form a class method in an ActiveRecord model. I tried a couple of > > > approaches none of which worked: > > > > 1) include ActionController::UrlWriter in the model class -- this > > > doesn''t work because the included methods are instance methods and > > > thus are not available in the class methods > > > > 2) According to the rails api you can access the helpers by using > > > ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ > > > classes/ActionController/UrlWriter.html). However that just doesn''t > > > work for me, I get the following error > > > > NoMethodError: undefined method `xxx_path'' for > > > ActionController::UrlWriter:Module > > > > If anyone has ANY ideas PLEASE let me know. > > > The helpers are defined in an anonymous module hidden inside the > > routing stuff. You can ask rails to include that module in a class for > > you, eg > > > class Foo > > include ActionController::UrlWriter > > def generate > > parent_categories_url :host => ''example.com'' > > end > > end > > ActionController::Routing::Routes.named_routes.install(Foo) > > > Foo.new.generate #=> "http://example.com/admin/parent_categories" > > > Fred
admin-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org
2009-Aug-15 13:39 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
Yea i guess it just seems silly to create a separate class who''s only purpose is to include ActionController::UrlWriter, especially since the rails documentation says that you can simply call ActionController::UrlWriter.xxx_path. On Aug 15, 9:32 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 15, 2:22 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > This only works for instance methods, my problem is that I need to be > > able to access xxxx_path from a class method... meaning: > > Well there''s nothing stopping you bundling up your url generation > stuff into its own class and then do UrlGenerator.new.generate (I''d > even reccomend it seing as install the routes will pollute your > model''s name space with loads of methods. > A small experiment shows that > ActionController::Routing::Routes.named_routes.install isn''t actually > needed - UrlWriter''s included method seems to call that for you > > Fred > > > > > class Foo > > include ActionController::UrlWriter > > def self.generate > > login_path :host => ''example.com'' > > end > > end > > ActionController::Routing::Routes.named_routes.install(Foo) > > > Foo.generate > > #=> NameError: undefined local variable or method `login_path'' for > > Foo:Class > > from (irb):4:in `generate'' > > from (irb):9 > > > On Aug 15, 9:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Aug 15, 1:52 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > > > So i''ve been banging my head against this for the better part of the > > > > last 5 hours and I have concluded that in my sleep deprived state I''m > > > > missing something really obvious and should just ask for help. > > > > > I need to be able to access named route helpers (XXX_path and XXX_URL) > > > > form a class method in an ActiveRecord model. I tried a couple of > > > > approaches none of which worked: > > > > > 1) include ActionController::UrlWriter in the model class -- this > > > > doesn''t work because the included methods are instance methods and > > > > thus are not available in the class methods > > > > > 2) According to the rails api you can access the helpers by using > > > > ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ > > > > classes/ActionController/UrlWriter.html). However that just doesn''t > > > > work for me, I get the following error > > > > > NoMethodError: undefined method `xxx_path'' for > > > > ActionController::UrlWriter:Module > > > > > If anyone has ANY ideas PLEASE let me know. > > > > The helpers are defined in an anonymous module hidden inside the > > > routing stuff. You can ask rails to include that module in a class for > > > you, eg > > > > class Foo > > > include ActionController::UrlWriter > > > def generate > > > parent_categories_url :host => ''example.com'' > > > end > > > end > > > ActionController::Routing::Routes.named_routes.install(Foo) > > > > Foo.new.generate #=> "http://example.com/admin/parent_categories" > > > > Fred
Matt Jones
2009-Aug-16 15:07 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
As in many cases, if it''s difficult to do in Rails, it''s probably wrong. Can you give more detail on why you''re generating URLs in a model rather than in the controller/view that''s sending them to the user? --Matt Jones On Aug 15, 9:22 am, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote:> This only works for instance methods, my problem is that I need to be > able to access xxxx_path from a class method... meaning: > > class Foo > include ActionController::UrlWriter > def self.generate > login_path :host => ''example.com'' > end > end > ActionController::Routing::Routes.named_routes.install(Foo) > > Foo.generate > #=> NameError: undefined local variable or method `login_path'' for > Foo:Class > from (irb):4:in `generate'' > from (irb):9 > > On Aug 15, 9:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Aug 15, 1:52 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > > So i''ve been banging my head against this for the better part of the > > > last 5 hours and I have concluded that in my sleep deprived state I''m > > > missing something really obvious and should just ask for help. > > > > I need to be able to access named route helpers (XXX_path and XXX_URL) > > > form a class method in an ActiveRecord model. I tried a couple of > > > approaches none of which worked: > > > > 1) include ActionController::UrlWriter in the model class -- this > > > doesn''t work because the included methods are instance methods and > > > thus are not available in the class methods > > > > 2) According to the rails api you can access the helpers by using > > > ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ > > > classes/ActionController/UrlWriter.html). However that just doesn''t > > > work for me, I get the following error > > > > NoMethodError: undefined method `xxx_path'' for > > > ActionController::UrlWriter:Module > > > > If anyone has ANY ideas PLEASE let me know. > > > The helpers are defined in an anonymous module hidden inside the > > routing stuff. You can ask rails to include that module in a class for > > you, eg > > > class Foo > > include ActionController::UrlWriter > > def generate > > parent_categories_url :host => ''example.com'' > > end > > end > > ActionController::Routing::Routes.named_routes.install(Foo) > > > Foo.new.generate #=> "http://example.com/admin/parent_categories" > > > Fred
admin-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org
2009-Aug-17 15:27 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
I am generating a URL for use with GoogleCheckout. I have a method in a model that uses the visitors shopping cart to construct an xml request to GoogleCheckout, in that request i need to include a callback url that GoogleCheckout can use to contact my webapp. On Aug 16, 11:07 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> As in many cases, if it''s difficult to do in Rails, it''s probably > wrong. Can you give more detail on why you''re generating URLs in a > model rather than in the controller/view that''s sending them to the > user? > > --Matt Jones > > On Aug 15, 9:22 am, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > This only works for instance methods, my problem is that I need to be > > able to access xxxx_path from a class method... meaning: > > > class Foo > > include ActionController::UrlWriter > > def self.generate > > login_path :host => ''example.com'' > > end > > end > > ActionController::Routing::Routes.named_routes.install(Foo) > > > Foo.generate > > #=> NameError: undefined local variable or method `login_path'' for > > Foo:Class > > from (irb):4:in `generate'' > > from (irb):9 > > > On Aug 15, 9:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Aug 15, 1:52 pm, "ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org" <ad...-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org> wrote: > > > > > So i''ve been banging my head against this for the better part of the > > > > last 5 hours and I have concluded that in my sleep deprived state I''m > > > > missing something really obvious and should just ask for help. > > > > > I need to be able to access named route helpers (XXX_path and XXX_URL) > > > > form a class method in an ActiveRecord model. I tried a couple of > > > > approaches none of which worked: > > > > > 1) include ActionController::UrlWriter in the model class -- this > > > > doesn''t work because the included methods are instance methods and > > > > thus are not available in the class methods > > > > > 2) According to the rails api you can access the helpers by using > > > > ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/ > > > > classes/ActionController/UrlWriter.html). However that just doesn''t > > > > work for me, I get the following error > > > > > NoMethodError: undefined method `xxx_path'' for > > > > ActionController::UrlWriter:Module > > > > > If anyone has ANY ideas PLEASE let me know. > > > > The helpers are defined in an anonymous module hidden inside the > > > routing stuff. You can ask rails to include that module in a class for > > > you, eg > > > > class Foo > > > include ActionController::UrlWriter > > > def generate > > > parent_categories_url :host => ''example.com'' > > > end > > > end > > > ActionController::Routing::Routes.named_routes.install(Foo) > > > > Foo.new.generate #=> "http://example.com/admin/parent_categories" > > > > Fred
Carter Gilchrist
2010-May-21 00:30 UTC
Re: ActionController::UrlWriter bug -- need help URGENTLY, thanks!
admin-Onn4oRDAYSFWk0Htik3J/w@public.gmane.org wrote:> I am generating a URL for use with GoogleCheckout. I have a method in > a model that uses the visitors shopping cart to construct an xml > request to GoogleCheckout, in that request i need to include a > callback url that GoogleCheckout can use to contact my webapp.I am in the same situation, needing to access the named routes from within a class method and am having no luck coming up with a logic solution. It seems that every post out there asking the same thing gets met with "if it''s so difficult, you probably shouldn''t be doing it." I usually agree with this, but generating a callback for any 3rd party api calls that get made at a class level seems like a pretty acceptable requirement. Did you ever come to a solution that worked for you admin@fotocera? -- 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.