This post addresses URLs generated by Ruby on Rails from a human factors point of view. Allow me to use Hieraki as an example. Hieraki is a great application. It was one of my first introductions to the power of Ruby on Rails. Scenario: When I navigated to Chapter 1 of a book the URL in my address bar was http://www.library.com/read/chapter/6. This inconsistancy ("Am I on Chapter 1 or 6?") threw me for a loop. I navigated back to the book page (http://www.library.com/read/book/2) to check the link again. With reduced confidence, I reclicked the "Chapter 1" link. At the bottom of Chapter 1 I moused over "next chapter", my status bar read http://www.library.com/read/chapter/13. Chapter 13?! I decided that there was no correlation between the URL and chapter. After more reading, I found a chapter that I wanted to return to. When I bookmarked the page the URL, again, caught me off guard: http://www.library.com/read/chapter/19. Chapter whatever (I''d given up on that part), but how does it know what book I''m reading? I cut and paste the URL into a completely different browser, and in disbelief, saw the correct page load. How on earth did it know what book I was reading? Grrrrr! From a RoR developer''s standpoint, these links make sense. It is requesting information by passing a unique id. From a user''s standpoint, these kind of URLs are confusing/misleading/counter-intuitive. My question: is there a way to write the controller (perhaps by adding to the belongs_to, and habtm) to allow for clearer URLs? For instance, being able to navigate to these pages: http://www.library.com/ <= information about library.com http://www.library.com/books <= list of books in library http://www.library.com/books/tomsawyer <= information about book "Tom Sawyer" http://www.library.com/books/tomsawyer/chapters <= list of chapters in book http://www.library.com/books/tomsawyer/chapters/1 <= content from chapter one using this table structure (with example data): table books: -id: 56 -title: tomsawyer table chapters: -id: 459 -sequence: 1 -book_id: 56 -content: "Tom!" The mysql request for http://www.library.com/books/tomsawyer/chapters/1 would look something like: "select content from chapters as c, books as b where b.title "tomsawyer" and c.sequence = "1" and b.id = c.book_id" Thinking out loud: chapters would be a subclass of books. Instances of books would be allowed to call the chapters class. Caveat: I was schooled in user centered design, and not computer science. I am new to Ruby and Rails. I''m having a great time learning along with everybody else.
On 25.5.2005, at 21:39, Nathan Colgate Clark wrote:> > My question: is there a way to write the controller (perhaps by > adding to the belongs_to, and habtm) to allow for clearer URLs?Yes. Like the previous poster suggested, you can use routes for this.> > For instance, being able to navigate to these pages: > > http://www.library.com/ <= information about library.com > http://www.library.com/books <= list of books in library > http://www.library.com/books/tomsawyer <= information about book "Tom > Sawyer" > http://www.library.com/books/tomsawyer/chapters <= list of chapters in > book > http://www.library.com/books/tomsawyer/chapters/1 <= content from > chapter onePut these in your config/routes.rb (before default routes so they''ll take precedence over the default ones): map.connect ''books'', :controller => "books", :action => "index" map.connect '':controller/:title'', :action => "book", :requirements => {:controller => /book/} map.connect '':controller/:title/chapters'', :action => "chapters", :requirements => {:controller => /book/} map.connect '':controller/:title/chapters/:sequence'', :action => "chapter", :requirements => {:controller => /book/, :sequence => /\d+/} Then you should of course have the according four actions in your books controller: index, book, chapters and chapter. Keep in mind also that the book title needs to be unique in this url scheme. And yes, I agree that it is better from the user standpoint than the current style. HTH, //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Have you heard about routes? http://wiki.rubyonrails.com/rails/show/Routes http://manuals.rubyonrails.com/read/book/9 http://wiki.rubyonrails.com/rails/show/HowToRouteGenericURLsToAController I''m in a hurry, sorry for this email being super short Cheers, Rob>This post addresses URLs generated by Ruby on Rails from a human >factors point of view. > >Allow me to use Hieraki as an example. Hieraki is a great >application. It was one of my first introductions to the power of >Ruby on Rails. > >Scenario: When I navigated to Chapter 1 of a book the URL in my >address bar was http://www.library.com/read/chapter/6. This >inconsistancy ("Am I on Chapter 1 or 6?") threw me for a loop. I >navigated back to the book page (http://www.library.com/read/book/2) >to check the link again. With reduced confidence, I reclicked the >"Chapter 1" link. At the bottom of Chapter 1 I moused over "next >chapter", my status bar read http://www.library.com/read/chapter/13. >Chapter 13?! I decided that there was no correlation between the URL >and chapter. After more reading, I found a chapter that I wanted to >return to. When I bookmarked the page the URL, again, caught me off >guard: http://www.library.com/read/chapter/19. Chapter whatever (I''d >given up on that part), but how does it know what book I''m reading? I >cut and paste the URL into a completely different browser, and in >disbelief, saw the correct page load. How on earth did it know what >book I was reading? Grrrrr! > > From a RoR developer''s standpoint, these links make sense. It is >requesting information by passing a unique id. > From a user''s standpoint, these kind of URLs are >confusing/misleading/counter-intuitive. > >My question: is there a way to write the controller (perhaps by >adding to the belongs_to, and habtm) to allow for clearer URLs? > >For instance, being able to navigate to these pages: > >http://www.library.com/ <= information about library.com >http://www.library.com/books <= list of books in library >http://www.library.com/books/tomsawyer <= information about book "Tom Sawyer" >http://www.library.com/books/tomsawyer/chapters <= list of chapters in book >http://www.library.com/books/tomsawyer/chapters/1 <= content from chapter one > >using this table structure (with example data): >table books: > -id: 56 > -title: tomsawyer >table chapters: > -id: 459 > -sequence: 1 > -book_id: 56 > -content: "Tom!" > >The mysql request for >http://www.library.com/books/tomsawyer/chapters/1 would look something >like: >"select content from chapters as c, books as b where b.title >"tomsawyer" and c.sequence = "1" and b.id = c.book_id" > >Thinking out loud: chapters would be a subclass of books. Instances >of books would be allowed to call the chapters class. > >Caveat: I was schooled in user centered design, and not computer >science. I am new to Ruby and Rails. I''m having a great time >learning along with everybody else. >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >. > > >