id like to have my site organized like this... nj.com/southern/restaurants/italian/3 to have a url like that, it seems i would need a controller called southern with an internal controller restaurants where 3 is the id of the item. thoughts? suggestions? thanks. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs
2007-Feb-06 20:20 UTC
Re: is having controller within controller bad practice?
That''s a perfectly valid URL, but it won''t mean ''controller-in-controller''. Rails routes are incredibly powerful, try something like this (near the bottom to allow for other custom routes to take precendence). map.connect '':region/:controller/:action/:id'' Then your :controller/:action will get: params = { :region => ''southern'', :id => 3 } Jason On 2/6/07, koloa <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > > id like to have my site organized like this... > > nj.com/southern/restaurants/italian/3 > > > to have a url like that, it seems i would need a controller called > southern with an internal controller restaurants where 3 is the id of > the item. > > thoughts? suggestions? thanks. > > -- > 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-/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 -~----------~----~----~----~------~----~------~--~---
Greg Donald
2007-Feb-06 20:54 UTC
Re: is having controller within controller bad practice?
On 2/6/07, koloa <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > id like to have my site organized like this... > > nj.com/southern/restaurants/italian/3 > > > to have a url like that, it seems i would need a controller called > southern with an internal controller restaurants where 3 is the id of > the item. > > thoughts? suggestions? thanks.I''ve been trying to make my URLs even more search engine friendly lately. I''ve gotten away from using integer values in the URL for anything except the admin/non-public side of my applications. I''ve been using a :base_name column combined that with some routing. In the model I do: before_save :base_name_from_title def base_name_from_title self.base_name = title.downcase.gsub( /\ and\ /, ''-'' ). gsub( /\ on\ /, ''-'' ). gsub( /[^a-zA-Z0-9\-]/, ''-'' ). gsub( /[\-]+/, ''-'' ). gsub( /[\-]$/, '''' ). gsub( /^[\-]/, '''' ) end "title" is whatever important wordy field I can pick out of a given model. It''s tough to pick sometimes especially if the field isn''t unique. I don''t have too many collisions on my personal blog though. Then I add a route: map.connect "category/:base_name", :controller => ''category'', :action => ''index'', :requirements => { :base_name => /[\w-]+/ }, :base_name => nil Then in the controller I look it up by base_name instead of by id: Category.find( :first, :conditions => [ ''base_name = ?'', params[:base_name] ] ) Don''t forget to index that text field! :) Then my URLs look like: http://destiney.com/category/programming-languages-ruby -- Greg Donald http://destiney.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-/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 -~----------~----~----~----~------~----~------~--~---
hi Jason and Greg, Thanks for the post...question though... lets say my category field in a model can be ''bbq'', ''chinese'', ''italian''...and id like to have the url localhost.com/nj/bbq or localhost.com/nj/southern/bbq where nj is a controller, southern is a :region field and bbq is a :mcategory field,,,,both are in my restaurant table.. do i define like this? map.connect '':controller/:region/:mcategory'', :action => ''view'' where view does the logic to determine what to pass in my find statement like def view NJ.find(:all, :conditions => "mcategory = ? and region = ?"params[:mcategory, params [:region]]) end the name ''view'', will that be part of the url? i would prefer to have it like localhost.com/nj/southern/bbq... thanks. Greg, id really like the way you did your titles. Jason Roelofs wrote:> That''s a perfectly valid URL, but it won''t mean > ''controller-in-controller''. > Rails routes are incredibly powerful, try something like this (near the > bottom to allow for other custom routes to take precendence). > > map.connect '':region/:controller/:action/:id'' > > Then your :controller/:action will get: > > params = { > :region => ''southern'', > :id => 3 > } > > Jason-- 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-/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 -~----------~----~----~----~------~----~------~--~---
whoa...i just tried it and it works and I can have the url that i like! wow that feature is really awesome.. i was about to brute force like so many controllers within each other. now i have to rethink my naming conventions of functions. my brain hurts.. thanks all for helping a noob. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
another quick question... i used to have a long case stament like this.. switch on ''food '' category case ''bbq'' ... if state == nj do find all in nj for bbq if state == pa do find etc.... case ''chinese'' .... if.... .... case ..... end switch on ''something else'' category ..... ... now because of the routes.rb power, i no longer need my case statement since i can have... map.connect '':state, :main_category, :sub_category'' so when presses a link on my site, it can go to a action that just has this? def getView @myObj = params;[:main_category].capitalize.find(:all, :conditions [''state = ? and subcategory = ?'', params[:state], params[:sub_category]]) end Thansk! -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs
2007-Feb-06 22:27 UTC
Re: is having controller within controller bad practice?
Yep, it would work pretty much like that. Anything on the URL gets split up according to route definitions and placed into params. Jason On 2/6/07, mixplate <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > another quick question... > > i used to have a long case stament like this.. > > > switch on ''food '' category > case ''bbq'' > ... > if state == nj > do find all in nj for bbq > if state == pa > do find etc.... > case ''chinese'' > .... > if.... > .... > case ..... > end > > switch on ''something else'' category > ..... > ... > > > now because of the routes.rb power, i no longer need my case statement > since i can have... > > map.connect '':state, :main_category, :sub_category'' > > > so when presses a link on my site, it can go to a action that just has > this? > > > def getView > @myObj = params;[:main_category].capitalize.find(:all, :conditions > [''state = ? and subcategory = ?'', params[:state], > params[:sub_category]]) > > end > > > > Thansk! > > > -- > 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-/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 -~----------~----~----~----~------~----~------~--~---
Hey Jason, Thanks for explaining this to me! i think i am now going to rewrite my app again for the third time cutting the code i have maybe by 30%! Jason Roelofs wrote:> Yep, it would work pretty much like that. Anything on the URL gets split > up > according to route definitions and placed into params. > > Jason-- 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-/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 -~----------~----~----~----~------~----~------~--~---