Hi Mats, On 2.8.2005, at 12:09, Mats Persson wrote:> > So I''m trying to achieve these, seemingly simple tasks: > > URL: site.com/chaptertitle > Route: /:chapter/ > > URL: site.com/chaptertitle/pagetitle > Route: /:chapter/:page > > URL: site.com/chaptertitle/subchaptertitle > Route: /:chapter/:subchapter/ > > URL: site.com/chaptertitle/subchaptertitle/pagetitle > Route: /:chapter/:subchapter/:page > > > I''ve been trying routes like this, but with no success. > > map.page ":chapter/:page", :controller => ''read'', :action => ''page'' > > > And using this code: url_for :controller => ''read'', :chapter => > @chapter, :page => @page > > I get this return: /read/index?chapter= chaptertitle&page= pagetitle > > but what I''m looking for is this: /chaptertitle/pagetitle/ > > What am I doing wrong ? Any help would be gratefully received.You need to put :action => ''page'' in your url_for (or link_to for that matter). Otherwise rails thinks you want to use the default action, which is ''index''. //jarkko -- Jarkko Laine jlaine.net odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org lists.rubyonrails.org/mailman/listinfo/rails
> > I''ve been trying routes like this, but with no success. > > > > map.page ":chapter/:page", :controller => ''read'', :action => ''page'' > > > > > > And using this code: url_for :controller => ''read'', :chapter => > > @chapter, :page => @page > > > > I get this return: /read/index?chapter= chaptertitle&page= pagetitle > > > > but what I''m looking for is this: /chaptertitle/pagetitle/ > > > > What am I doing wrong ? Any help would be gratefully received.Assuming that you''re using Rails 0.13.1, you can use named routes. Since your route was created with ''map.page'', you can use: page_url :chapter => @chapter, :page => @page Since the controller and action are specified in the named route, there''s no need to specify them again. -- rick techno-weenie.net
On 2 Aug 2005, at 12:46, Rick Olson wrote:> Assuming that you''re using Rails 0.13.1, you can use named routes. > Since your route was created with ''map.page'', you can use: > page_url :chapter => @chapter, :page => @pageYeah, i got that one going after my first post. However, my problem is seemingly greater than that as it does not really give the complete solution as far as i can tell. This is the problem I''m trying to work out/solve: I have a set of URL''s that can either be: 1. URL: site.com/chaptertitle 2. URL: site.com/chaptertitle/pagetitle 3. URL: site.com/chaptertitle/subchaptertitle 4. URL: site.com/chaptertitle/subchaptertitle/pagetitle So if I have a chapter called "RubyOnRails" and a subchapter called "Routes" and then a page called "How To" then the url should become site.com/rubyonrails/routes/how-to and the correct page should be loaded from the db. While handling that, it should also handle a chapter called "RubyOnRails" and a page called "introduction". At various points during my trials I could get a few of these thing working, but trying to get them all working was/is not possible, and now even I am confused with my own code. in routes.rb:: map.chapter ":chapter/:subchapter_or_page/:page", :controller => ''read'', :action => ''chapterurl'', :requirements => { :chapter => "/(\w-?) +/", :subchapter_or_page => "/(\w-?)+/", :page => "/(\w-?)+/" }, :page => nil, :subchapter_or_page => nil map.page ":chapter/:page", :controller => ''read'', :action => ''pageurl'', :requirements => { :chapter => "/(\w-?) +/", :page => "/(\w-?)+/" }, :page => nil If I comment out the requirements bit, then they kind of work, but the wrong one works most of the time. The KEY POINT here, is the URL STRUCTURE. It is the defining item, and everything has to fit within that structure. So if you can solve this conundrum, then please help me. Kind regards, Mats ---- "TextMate, coding with an incredible sense of joy and ease" - macromates.com -
On 2.8.2005, at 16:52, Mats Persson wrote:> > On 2 Aug 2005, at 12:46, Rick Olson wrote: >> Assuming that you''re using Rails 0.13.1, you can use named routes. >> Since your route was created with ''map.page'', you can use: >> page_url :chapter => @chapter, :page => @page > > Yeah, i got that one going after my first post. > > However, my problem is seemingly greater than that as it does not > really give the complete solution as far as i can tell. This is the > problem I''m trying to work out/solve: > > I have a set of URL''s that can either be: > 1. URL: site.com/chaptertitle > 2. URL: site.com/chaptertitle/pagetitle > 3. URL: site.com/chaptertitle/subchaptertitleThe problem is that there''s no way to distinguish 2 from 3. You need to have something unique in a route in order for it to work. Now, if you have the routes in that order, the route 2 will always be triggered even for cases where you''d want to access the subchapter page.> 4. URL: site.com/chaptertitle/subchaptertitle/pagetitle > > So if I have a chapter called "RubyOnRails" and a subchapter called > "Routes" and then a page called "How To" then the url should become > site.com/rubyonrails/routes/how-to and the correct page should be > loaded from the db. > > While handling that, it should also handle a chapter called > "RubyOnRails" and a page called "introduction". > > At various points during my trials I could get a few of these thing > working, but trying to get them all working was/is not possible, and > now even I am confused with my own code. > > in routes.rb:: > > map.chapter ":chapter/:subchapter_or_page/:page", > :controller => ''read'', > :action => ''chapterurl'', > :requirements => { :chapter => > "/(\w-?)+/", :subchapter_or_page => "/(\w-?)+/", :page => "/(\w-?)+/" > }, > :page => nil, > :subchapter_or_page => nil > > map.page ":chapter/:page", > :controller => ''read'', > :action => ''pageurl'', > :requirements => { :chapter => > "/(\w-?)+/", :page => "/(\w-?)+/" }, > :page => nil > > If I comment out the requirements bit, then they kind of work, but the > wrong one works most of the time.See above for the reason. The routes are always parsed in order you define them. Therefore everything that would trigger the latter route triggers also the first one (because you have a default value of nil for the page).> > The KEY POINT here, is the URL STRUCTURE. It is the defining item, and > everything has to fit within that structure. So if you can solve this > conundrum, then please help me.If the URL structure needs to be intact, you can''t solve this with pure routes. You can, however, create a single action that handles both cases. That action could then check for a page and if it doesn''t exist, go on to find a subchapter with that name. The action can use a different template depending if it is showing a page or a subchapter. HTH, //jarkko -- Jarkko Laine jlaine.net odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org lists.rubyonrails.org/mailman/listinfo/rails
On 2 Aug 2005, at 15:07, Jarkko Laine wrote:>> On 2.8.2005, at 16:52, Mats Persson wrote: >> I have a set of URL''s that can either be: >> 1. URL: site.com/chaptertitle >> 2. URL: site.com/chaptertitle/pagetitle >> 3. URL: site.com/chaptertitle/subchaptertitle >> 4. URL: site.com/chaptertitle/subchaptertitle/pagetitle > > The problem is that there''s no way to distinguish 2 from 3. You > need to have something unique in a route in order for it to work. > Now, if you have the routes in that order, the route 2 will always > be triggered even for cases where you''d want to access the > subchapter page.Yeah, the ordering of routes was kind of confusing, and does still cause some issues. But hopefully those can be worked out.>> The KEY POINT here, is the URL STRUCTURE. It is the defining item, >> and everything has to fit within that structure. So if you can >> solve this conundrum, then please help me. > > If the URL structure needs to be intact, you can''t solve this with > pure routes. You can, however, create a single action that handles > both cases. That action could then check for a page and if it > doesn''t exist, go on to find a subchapter with that name. The > action can use a different template depending if it is showing a > page or a subchapter.Thanks for that simple straight answer, I will remember that now, and only wish I''d known that before I spent hours trying to achieve something like it. ;-) (And yes, I''ve read the wiki & the book, but I''m crap at reading and comprehending semi-''hidden'' info. I prefer things very clear and concise or explained to me. ) Kind regards, Mats ---- "TextMate, coding with an incredible sense of joy and ease" - macromates.com -