Eric D. Nielsen
2006-Jul-23 17:44 UTC
[Rails] Anything like with_scope for ActionController?
There are areas of my application that "want" to use a url structure like domain/<human_readable_param>/<Module>/<Controller>/<Action>/<Id> Please note that the human readable parameter I need to inject is NOT related to the <id> being used by the action/controller pair. It''s identifying a container object whose contains are operated on by the actions. I currently have this working with routes, however I need to tack on an extra parameter to all the url generating functions (url_for, form_start_tag, named_route_url...) to get the mapping to pick up the custom URL parameter. Having to remember to append :short_name=>@my_object.short_name (with or without to_param magic), is getting annoying and the repetition is getting ugly. Is there anything like ActiveRecord''s with_scope function to "bind" this extra parameter once and make ActionController smart about propagating the extra parameter everywhere? Thank you. Eric
Just put it the shared logic in application_controller.rb. - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 23, 2006, at 10:44 AM, Eric D. Nielsen wrote:> There are areas of my application that "want" to use a url > structure like > > domain/<human_readable_param>/<Module>/<Controller>/<Action>/<Id> > > Please note that the human readable parameter I need to inject is > NOT related to the <id> being used by the action/controller pair. > It''s identifying a container object whose contains are operated on > by the actions. > > I currently have this working with routes, however I need to tack > on an extra parameter to all the url generating functions (url_for, > form_start_tag, named_route_url...) to get the mapping to pick up > the custom URL parameter. Having to remember to > append :short_name=>@my_object.short_name (with or without to_param > magic), is getting annoying and the repetition is getting ugly. > > Is there anything like ActiveRecord''s with_scope function to "bind" > this extra parameter once and make ActionController smart about > propagating the extra parameter everywhere? > > Thank you. > > Eric > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Sorry, I meant to say in application.rb and use a before filter to call for each necessary method. - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 23, 2006, at 2:01 PM, Dan Kohn wrote:> Just put it the shared logic in application_controller.rb. > > - dan > -- Dan Kohn <mailto:dan@dankohn.com> > <http://www.dankohn.com/> <tel:+1-415-233-1000> > > > > On Jul 23, 2006, at 10:44 AM, Eric D. Nielsen wrote: > >> There are areas of my application that "want" to use a url >> structure like >> >> domain/<human_readable_param>/<Module>/<Controller>/<Action>/<Id> >> >> Please note that the human readable parameter I need to inject is >> NOT related to the <id> being used by the action/controller pair. >> It''s identifying a container object whose contains are operated on >> by the actions. >> >> I currently have this working with routes, however I need to tack >> on an extra parameter to all the url generating functions >> (url_for, form_start_tag, named_route_url...) to get the mapping >> to pick up the custom URL parameter. Having to remember to >> append :short_name=>@my_object.short_name (with or without >> to_param magic), is getting annoying and the repetition is getting >> ugly. >> >> Is there anything like ActiveRecord''s with_scope function to >> "bind" this extra parameter once and make ActionController smart >> about propagating the extra parameter everywhere? >> >> Thank you. >> >> Eric >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >
Eric D. Nielsen
2006-Jul-23 21:16 UTC
[Rails] Anything like with_scope for ActionController?
On Jul 23, 2006, at 5:01 PM, Dan Kohn wrote:> Just put it the shared logic in application_controller.rb.I can''t find a clean way to do that. URLs are generated by Rails in several different class hierachies and there doesn''t seem to be one or two key functions to override. As a result even if I add custom functions to application_controller its easily possible to miss a function. Eric> On Jul 23, 2006, at 10:44 AM, Eric D. Nielsen wrote: > >> There are areas of my application that "want" to use a url >> structure like >> >> domain/<human_readable_param>/<Module>/<Controller>/<Action>/<Id> >> >> Please note that the human readable parameter I need to inject is >> NOT related to the <id> being used by the action/controller pair. >> It''s identifying a container object whose contains are operated on >> by the actions. >> >> I currently have this working with routes, however I need to tack >> on an extra parameter to all the url generating functions >> (url_for, form_start_tag, named_route_url...) to get the mapping >> to pick up the custom URL parameter. Having to remember to >> append :short_name=>@my_object.short_name (with or without >> to_param magic), is getting annoying and the repetition is getting >> ugly. >> >> Is there anything like ActiveRecord''s with_scope function to >> "bind" this extra parameter once and make ActionController smart >> about propagating the extra parameter everywhere? >> >> Thank you. >> >> Eric >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >
Yeah, I wasn''t suggesting any fancy Ruby magic of opening up Rails classes and adding an optional parameter. I was just saying that you could create a bunch of your own methods like my_url_for and my_form_tag that would call the real functions with your parameter added. You would put your methods in application.rb. It''s not as DRY as opening up one class and getting the functionality for all methods, but it''s still DRY. If you use my naming scheme and "miss a function" you''ll get an immediate application error. - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 23, 2006, at 2:11 PM, Eric D. Nielsen wrote:> On Jul 23, 2006, at 5:01 PM, Dan Kohn wrote: >> Just put it the shared logic in application_controller.rb. > > I can''t find a clean way to do that. URLs are generated by Rails > in several different class hierachies and there doesn''t seem to be > one or two key functions to override. As a result even if I add > custom functions to application_controller its easily possible to > miss a function. > > Eric > > >> On Jul 23, 2006, at 10:44 AM, Eric D. Nielsen wrote: >> >>> There are areas of my application that "want" to use a url >>> structure like >>> >>> domain/<human_readable_param>/<Module>/<Controller>/<Action>/<Id> >>> >>> Please note that the human readable parameter I need to inject is >>> NOT related to the <id> being used by the action/controller >>> pair. It''s identifying a container object whose contains are >>> operated on by the actions. >>> >>> I currently have this working with routes, however I need to tack >>> on an extra parameter to all the url generating functions >>> (url_for, form_start_tag, named_route_url...) to get the mapping >>> to pick up the custom URL parameter. Having to remember to >>> append :short_name=>@my_object.short_name (with or without >>> to_param magic), is getting annoying and the repetition is >>> getting ugly. >>> >>> Is there anything like ActiveRecord''s with_scope function to >>> "bind" this extra parameter once and make ActionController smart >>> about propagating the extra parameter everywhere? >>> >>> Thank you. >>> >>> Eric >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>