On the subject of http://dev.rubyonrails.org/ticket/1138 As we are now revamping the routing, maybe it might be sensible to create a decoupled url_for that only depends on the Routes and bails if no routes have been defined? I''d say the inability to use url_for in the Mailer is more of an annoying coupling problem than a roadblock. There should be a way to generate URLs without a controller. -- Julian ''Julik'' Tarkhanov please send all personal mail to me at julik.nl
On Jun 5, 2006, at 10:32 AM, Julian ''Julik'' Tarkhanov wrote:> On the subject of http://dev.rubyonrails.org/ticket/1138 > > As we are now revamping the routing, maybe it might be sensible to > create a decoupled url_for that only depends on the Routes and > bails if no routes have been defined? I''d say the inability to use > url_for in the Mailer is more of an annoying coupling problem than > a roadblock. There should be a way to generate URLs without a > controller.I agree. However, when people use a Mailer within the context of a request, they typically expect url_for to operate like it does in a view--that is, to reuse request parameters. That''s where the dependency on the controller comes in--the url rewriter needs a request instance to operate. If a clean way can be devised to allow it to work as expected in both environments (request vs. cron, for example), I''d certainly consider it. As you said, it is not a roadblock, merely a coupling problem. Please do investigate how to decouple these. - Jamis
On 5-jun-2006, at 19:06, Jamis Buck wrote:> On Jun 5, 2006, at 10:32 AM, Julian ''Julik'' Tarkhanov wrote: > >> On the subject of http://dev.rubyonrails.org/ticket/1138 >> >> As we are now revamping the routing, maybe it might be sensible to >> create a decoupled url_for that only depends on the Routes and >> bails if no routes have been defined? I''d say the inability to use >> url_for in the Mailer is more of an annoying coupling problem than >> a roadblock. There should be a way to generate URLs without a >> controller. > > I agree. However, when people use a Mailer within the context of a > request, they typically expect url_for to operate like it does in a > view--that is, to reuse request parameters.Are you certain about that? I for one expect the opposite - that it would be an "absolute" URL. However I understand where it''s headed (for instance your apps would love to preserve the subdomain when sending mails). Will investigate. -- Julian ''Julik'' Tarkhanov please send all personal mail to me at julik.nl
On Jun 5, 2006, at 1:47 PM, Julian ''Julik'' Tarkhanov wrote:> Are you certain about that? I for one expect the opposite - that it > would be an "absolute" URL. However I understand where it''s headed > (for instance your apps would love to preserve the subdomain when > sending mails). Will investigate.I think it''s reasonable to require all the parameters be present for AM url_for''s... but we still need to access some request vars, such as request.host for obvious reasons. _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
On 5-jun-2006, at 20:36, Nicholas Seckar wrote:> > On Jun 5, 2006, at 1:47 PM, Julian ''Julik'' Tarkhanov wrote: > >> Are you certain about that? I for one expect the opposite - that >> it would be an "absolute" URL. However I understand where it''s >> headed (for instance your apps would love to preserve the >> subdomain when sending mails). Will investigate. > > I think it''s reasonable to require all the parameters be present > for AM url_for''s... but we still need to access some request vars, > such as request.host for obvious reasons.Ok, I think I''ll tackle it the following way. 1. specifically for ActionMailer ActionMailer::Base.current_relative_controller = current_controller (as a before_filter for the ActionController::Base) and forward the url_for directly to the instance of it Seems like a proper fit for Railties as a mixin for AM. 2. for all the other cases that might need the infrastructure ActionController::Routing::Routes.default_request_parameters = {:host => ''bla''} and retrieve it through something like ActionController::Routing::Routes.default_url_for(*params) Wat do you think? -- Julian ''Julik'' Tarkhanov please send all personal mail to me at julik.nl
+1 to this idea in general. On 06/06/2006, at 7:59 AM, Julian ''Julik'' Tarkhanov wrote:> Ok, I think I''ll tackle it the following way. > > 1. specifically for ActionMailer > ActionMailer::Base.current_relative_controller = > current_controller (as a before_filter for the ActionController::Base) > and forward the url_for directly to the instance of it > Seems like a proper fit for Railties as a mixin for AM.If I call my ActionMailer from a unit test (or runner script, or the console, etc.) then there is no controller. Would url_for in the ActionMailer then fail? (I''ve attached the code I currently use below, and it suffers from this problem.)> 2. for all the other cases that might need the infrastructure > ActionController::Routing::Routes.default_request_parameters = > {:host => ''bla''} > and retrieve it through something like > ActionController::Routing::Routes.default_url_for(*params)This is sensible. url_for should really be defined as part of the routing infrastructure with some configurable values for host, etc. ActionController and ActionMailer should then have url_for instance methods that call out to the url_for in routing. In the case of ActionController, it would pass its own values for host, protocol, etc. In the case of ActionMailer, it would use the defaults from the config. (You could, of course, manually pass your own values in if you need to generate them dynamically, as would be the case if you need to generate links to more than one host.) If my somewhat limited understanding of the internals of Rails routing means I''ve suggested something dumb above, please feel free to straighten me out. :) Pete Yandell http://9cays.com/ This is the code I currently use to solve this problem. In a before_filter I do UrlGenerator.controller = self, and then I can call UrlGenerator.url_for from my mailer to generate URLs. (I pinched this idea from an open source Rails project somewhere, but I don''t recall where. My apologies to the original author.) class UrlGenerator @@controller ||= nil def self.controller=(controller) @@controller = controller end def self.url_for(options) @@controller.url_for(options) if @@controller end end