Hi all, With ye olde routes, you could do stuff like: map.connect ''/foo/:id'', :controller => ''bar'', :type => ''foo'' but you can''t pass arbitrary params into #resources: map.resources :foo, :controller => ''bar'', :type => ''foo'' # bonk! the :controller key works fine, but :type is just ignored. This would be very handy for an STI-backed controller I have, to change a url like "/bar/new?type=foo" to "/foo/new" but still handle it with BarController. I''ve seen a few people ask this question, eg. http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/ab38af850896baeb/384c7c3ef2a9f9cb?lnk=gst&q=params+resources#384c7c3ef2a9f9cb but no answers. I''ve tried a bunch of stuff with :requirements, :as, :path_prefix, and nesting, but all to no avail. If anyone has any advice, I''d be most grateful. Thanks, Ian --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ian- It might be helpful if you gave a few more examples of what you want to accomplish. Are you trying to have the following routes all point to the "new" method on the same controller? /foo/new /bar/new /somethingelse/new Greg DeVore --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sure. You have it basically right, but let me elaborate. I have models like so: class Papa < ActiveRecord::Base; abstract_class = true; end class Daughter < Papa; end class Son < Papa; end I have this controller: class PapaController < ApplicationController def new; @model = new_model; end def new_model params[:type].classify.constantize.new # there''s more safety logic here in reality, but this is the gist end end I started with the default route: map.resources :papa So, then, I can create a new son by doing GET /papa/new?type=son, which is fine, but not ideal. Ideally, I would do GET /son/new, but it would still use the Papa controller, and set params[:type] to "son". This is what I have right now: map.resources :papa, :path_prefix => ''/:type'' which allows me to GET /son/papa/new, and create links like papa_path(@model.type.to_s.downcase, @model). So the question is how to make these specs pass: params_from(:get, "/son/new").should == {:controller => ''papa'', :action => ''new'', :type => ''son''} params_from(:get, "/daughter/new").should == {:controller => ''papa'', :action => ''new'', :type => ''daughter''} Is that clearer? Thanks, Ian On Wed, Oct 1, 2008 at 6:19 AM, gdevore <greg.devore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Ian- > It might be helpful if you gave a few more examples of what you want > to accomplish. Are you trying to have the following routes all point > to the "new" method on the same controller? > > /foo/new > /bar/new > /somethingelse/new > > > Greg DeVore > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ian- I don''t know how you would do that in the way you are describing. One possibility though would be to do this: map.resources :sons, :controller => "papas" map.resources :daughters, :controller => "papas" This will point both routes to the papas controller. You won''t see "sons" or "daughters" come through as params. But you can query request.env[''REQUEST_URI''] and you would get back something like ''/ sons/new/''. So: 1. request.env[''REQUEST_URI''].split("/") will give you an array ["","sons","new"] 2. access [1] of the array and you will have your parameter. A little convoluted but I think that it would do what you want. Greg On Oct 1, 2008, at 6:12 PM, Ian Smith-Heisters wrote:> > Sure. You have it basically right, but let me elaborate. > > I have models like so: > > class Papa < ActiveRecord::Base; abstract_class = true; end > class Daughter < Papa; end > class Son < Papa; end > > I have this controller: > > class PapaController < ApplicationController > def new; @model = new_model; end > def new_model > params[:type].classify.constantize.new # there''s more safety logic > here in reality, but this is the gist > end > end > > I started with the default route: > > map.resources :papa > > So, then, I can create a new son by doing GET /papa/new?type=son, > which is fine, but not ideal. Ideally, I would do GET /son/new, but it > would still use the Papa controller, and set params[:type] to "son". > This is what I have right now: > > map.resources :papa, :path_prefix => ''/:type'' > > which allows me to GET /son/papa/new, and create links like > papa_path(@model.type.to_s.downcase, @model). > > So the question is how to make these specs pass: > > params_from(:get, "/son/new").should == {:controller => ''papa'', > :action => ''new'', :type => ''son''} > params_from(:get, "/daughter/new").should == {:controller => ''papa'', > :action => ''new'', :type => ''daughter''} > > Is that clearer? > > Thanks, > Ian >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It sounds like you want to be using nested resources. Try something like this map.resources :papas, :has_many => [:sons, :daughters] For more info read... http://api.rubyonrails.org/classes/ActionController/Resources.html On Oct 1, 10:35 pm, Greg DeVore <greg.dev...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ian- > I don''t know how you would do that in the way you are describing. One > possibility though would be to do this: > > map.resources :sons, :controller => "papas" > map.resources :daughters, :controller => "papas" > > This will point both routes to the papas controller. You won''t see > "sons" or "daughters" come through as params. But you can query > request.env[''REQUEST_URI''] and you would get back something like ''/ > sons/new/''. > > So: > 1. request.env[''REQUEST_URI''].split("/") will give you an array > ["","sons","new"] > 2. access [1] of the array and you will have your parameter. > > A little convoluted but I think that it would do what you want. > > Greg > > On Oct 1, 2008, at 6:12 PM, Ian Smith-Heisters wrote: > > > > > Sure. You have it basically right, but let me elaborate. > > > I have models like so: > > > class Papa < ActiveRecord::Base; abstract_class = true; end > > class Daughter < Papa; end > > class Son < Papa; end > > > I have this controller: > > > class PapaController < ApplicationController > > def new; @model = new_model; end > > def new_model > > params[:type].classify.constantize.new # there''s more safety logic > > here in reality, but this is the gist > > end > > end > > > I started with the default route: > > > map.resources :papa > > > So, then, I can create a new son by doing GET /papa/new?type=son, > > which is fine, but not ideal. Ideally, I would do GET /son/new, but it > > would still use the Papa controller, and set params[:type] to "son". > > This is what I have right now: > > > map.resources :papa, :path_prefix => ''/:type'' > > > which allows me to GET /son/papa/new, and create links like > > papa_path(@model.type.to_s.downcase, @model). > > > So the question is how to make these specs pass: > > > params_from(:get, "/son/new").should == {:controller => ''papa'', > > :action => ''new'', :type => ''son''} > > params_from(:get, "/daughter/new").should == {:controller => ''papa'', > > :action => ''new'', :type => ''daughter''} > > > Is that clearer? > > > Thanks, > > Ian--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Wouldn''t that give me "/papas/1/sons/new"? Not exactly what I''m looking for if I understand it correctly, but thanks. On Thu, Oct 2, 2008 at 9:18 AM, Dave S <DaveShenk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > It sounds like you want to be using nested resources. Try something > like this > > map.resources :papas, :has_many => [:sons, :daughters] > > For more info read... > > http://api.rubyonrails.org/classes/ActionController/Resources.html > > On Oct 1, 10:35 pm, Greg DeVore <greg.dev...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Ian- >> I don''t know how you would do that in the way you are describing. One >> possibility though would be to do this: >> >> map.resources :sons, :controller => "papas" >> map.resources :daughters, :controller => "papas" >> >> This will point both routes to the papas controller. You won''t see >> "sons" or "daughters" come through as params. But you can query >> request.env[''REQUEST_URI''] and you would get back something like ''/ >> sons/new/''. >> >> So: >> 1. request.env[''REQUEST_URI''].split("/") will give you an array >> ["","sons","new"] >> 2. access [1] of the array and you will have your parameter. >> >> A little convoluted but I think that it would do what you want. >> >> Greg >> >> On Oct 1, 2008, at 6:12 PM, Ian Smith-Heisters wrote: >> >> >> >> > Sure. You have it basically right, but let me elaborate. >> >> > I have models like so: >> >> > class Papa < ActiveRecord::Base; abstract_class = true; end >> > class Daughter < Papa; end >> > class Son < Papa; end >> >> > I have this controller: >> >> > class PapaController < ApplicationController >> > def new; @model = new_model; end >> > def new_model >> > params[:type].classify.constantize.new # there''s more safety logic >> > here in reality, but this is the gist >> > end >> > end >> >> > I started with the default route: >> >> > map.resources :papa >> >> > So, then, I can create a new son by doing GET /papa/new?type=son, >> > which is fine, but not ideal. Ideally, I would do GET /son/new, but it >> > would still use the Papa controller, and set params[:type] to "son". >> > This is what I have right now: >> >> > map.resources :papa, :path_prefix => ''/:type'' >> >> > which allows me to GET /son/papa/new, and create links like >> > papa_path(@model.type.to_s.downcase, @model). >> >> > So the question is how to make these specs pass: >> >> > params_from(:get, "/son/new").should == {:controller => ''papa'', >> > :action => ''new'', :type => ''son''} >> > params_from(:get, "/daughter/new").should == {:controller => ''papa'', >> > :action => ''new'', :type => ''daughter''} >> >> > Is that clearer? >> >> > Thanks, >> > Ian > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sigh, yeah, there''s always that way. So unrailsy... but probably what I''ll end up doing. Or I could patch Resources to make it push any unclaimed options into params. But parsing the URI by hand is probably easier. On Wed, Oct 1, 2008 at 8:35 PM, Greg DeVore <greg.devore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Ian- > I don''t know how you would do that in the way you are describing. One > possibility though would be to do this: > > map.resources :sons, :controller => "papas" > map.resources :daughters, :controller => "papas" > > This will point both routes to the papas controller. You won''t see > "sons" or "daughters" come through as params. But you can query > request.env[''REQUEST_URI''] and you would get back something like ''/ > sons/new/''. > > So: > 1. request.env[''REQUEST_URI''].split("/") will give you an array > ["","sons","new"] > 2. access [1] of the array and you will have your parameter. > > A little convoluted but I think that it would do what you want. > > Greg > > > On Oct 1, 2008, at 6:12 PM, Ian Smith-Heisters wrote: > >> >> Sure. You have it basically right, but let me elaborate. >> >> I have models like so: >> >> class Papa < ActiveRecord::Base; abstract_class = true; end >> class Daughter < Papa; end >> class Son < Papa; end >> >> I have this controller: >> >> class PapaController < ApplicationController >> def new; @model = new_model; end >> def new_model >> params[:type].classify.constantize.new # there''s more safety logic >> here in reality, but this is the gist >> end >> end >> >> I started with the default route: >> >> map.resources :papa >> >> So, then, I can create a new son by doing GET /papa/new?type=son, >> which is fine, but not ideal. Ideally, I would do GET /son/new, but it >> would still use the Papa controller, and set params[:type] to "son". >> This is what I have right now: >> >> map.resources :papa, :path_prefix => ''/:type'' >> >> which allows me to GET /son/papa/new, and create links like >> papa_path(@model.type.to_s.downcase, @model). >> >> So the question is how to make these specs pass: >> >> params_from(:get, "/son/new").should == {:controller => ''papa'', >> :action => ''new'', :type => ''son''} >> params_from(:get, "/daughter/new").should == {:controller => ''papa'', >> :action => ''new'', :type => ''daughter''} >> >> Is that clearer? >> >> Thanks, >> Ian >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s what I cooked, up. Wasn''t that hard, and appears to work so far... $ cat lib/param_resources.rb module ParamResources def self.included base super base base.alias_method_chain :action_options_for, :params end def action_options_for_with_params action, resource, method=nil action_options_for_without_params(action, resource, method). merge(resource.options[:params] || {}) end end $ cat config/routes.rb ActionController::Routing::RouteSet::Mapper.send :include, ParamResources ActionController::Routing::Routes.draw do |map| map.resources :papa map.resources :son, :controller => :papa, :params => {:type => "son"} map.resources :daughter, :controller => :papa, :params => {:type => "daughter"} <snip> I''d be interested in peoples'' takes on it. Thanks, Ian On Thu, Oct 2, 2008 at 11:46 AM, Ian Smith-Heisters <i@0x09.com> wrote:> Sigh, yeah, there''s always that way. So unrailsy... but probably what > I''ll end up doing. Or I could patch Resources to make it push any > unclaimed options into params. But parsing the URI by hand is probably > easier. > > On Wed, Oct 1, 2008 at 8:35 PM, Greg DeVore <greg.devore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> Ian- >> I don''t know how you would do that in the way you are describing. One >> possibility though would be to do this: >> >> map.resources :sons, :controller => "papas" >> map.resources :daughters, :controller => "papas" >> >> This will point both routes to the papas controller. You won''t see >> "sons" or "daughters" come through as params. But you can query >> request.env[''REQUEST_URI''] and you would get back something like ''/ >> sons/new/''. >> >> So: >> 1. request.env[''REQUEST_URI''].split("/") will give you an array >> ["","sons","new"] >> 2. access [1] of the array and you will have your parameter. >> >> A little convoluted but I think that it would do what you want. >> >> Greg >> >> >> On Oct 1, 2008, at 6:12 PM, Ian Smith-Heisters wrote: >> >>> >>> Sure. You have it basically right, but let me elaborate. >>> >>> I have models like so: >>> >>> class Papa < ActiveRecord::Base; abstract_class = true; end >>> class Daughter < Papa; end >>> class Son < Papa; end >>> >>> I have this controller: >>> >>> class PapaController < ApplicationController >>> def new; @model = new_model; end >>> def new_model >>> params[:type].classify.constantize.new # there''s more safety logic >>> here in reality, but this is the gist >>> end >>> end >>> >>> I started with the default route: >>> >>> map.resources :papa >>> >>> So, then, I can create a new son by doing GET /papa/new?type=son, >>> which is fine, but not ideal. Ideally, I would do GET /son/new, but it >>> would still use the Papa controller, and set params[:type] to "son". >>> This is what I have right now: >>> >>> map.resources :papa, :path_prefix => ''/:type'' >>> >>> which allows me to GET /son/papa/new, and create links like >>> papa_path(@model.type.to_s.downcase, @model). >>> >>> So the question is how to make these specs pass: >>> >>> params_from(:get, "/son/new").should == {:controller => ''papa'', >>> :action => ''new'', :type => ''son''} >>> params_from(:get, "/daughter/new").should == {:controller => ''papa'', >>> :action => ''new'', :type => ''daughter''} >>> >>> Is that clearer? >>> >>> Thanks, >>> Ian >>> >> >> >> >> >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---