Hi all, I have gone through a few of the Rail tutorials available and am looking at writing a small test application of my own. I want to do a catalogue of some sort, were objects in the catalogue are categorized in a three (or more) level hierarchy. So far all the examples I have seen work by passing around ''id''-s. But for the sake of nice URLs I would like to use the name of objects and categories. So instead of /object/show/3 I would like to use /object/show/my_obj_name. How would I go about doing that? Many thanks in advance, Nicky
ActiveRecord dynamically generates find methods for your properties. So, if you have a ''name'' property, you can use Model.find_by_name. If you''re doing this, however, you will want to validate that name is unique of course. On Wed, 23 Feb 2005 21:53:05 +0100, Nickolay Kolev <nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org> wrote:> Hi all, > > I have gone through a few of the Rail tutorials available and am > looking at writing a small test application of my own. I want to do a > catalogue of some sort, were objects in the catalogue are categorized > in a three (or more) level hierarchy. > > So far all the examples I have seen work by passing around ''id''-s. But > for the sake of nice URLs I would like to use the name of objects and > categories. > > So instead of /object/show/3 I would like to use > /object/show/my_obj_name. > > How would I go about doing that? > > Many thanks in advance, > Nicky > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- rick http://techno-weenie.net
Nickolay Kolev:> So instead of /object/show/3 I would like to use /object/show/my_obj_name. > > How would I go about doing that?There are lots of possiblities, depending on what you want to do. For starters how about: @page = Page.find_first [''title = ?'', @params[''id'']] Check all the methods that start with find* on the api page: http://api.rubyonrails.com Good luck Sascha
> @page = Page.find_first [''title = ?'', @params[''id'']]Looks like it would work. I will give it a try. Thanks! -- Nicky
On 23-Feb-2005, at 15:53, Nickolay Kolev wrote:> So far all the examples I have seen work by passing around ''id''-s. But > for the sake of nice URLs I would like to use the name of objects and > categories. >Don''t know how to work it into URLs, but there are nice "pseudo" methods you can use, for instance, if you table has a column called "name", then you can do this: @people = People.find_by_name( "Johnson" ) As someone else posted, look through the documentation at http://rubyonrails.org . Regards, JJ
On Wed, 23 Feb 2005 21:53:05 +0100, Nickolay Kolev <nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org> wrote:> So instead of /object/show/3 I would like to use > /object/show/my_obj_name. > > How would I go about doing that?First, you probably want a RoR with Routes, so either try the beta gems or wait for the next release RSN (tomorrow?). Define a route like so (or similar): map.connect '':year/:slug'', :controller => ''entry'', :action => ''show'', :year => /\d{4}/ This basically matches URLs of the form /2005/some-slug-here. A very nice thing about this is that link_to will automatically generate correct and "nice" links, so ... link_to "foo", {:action => "show", :controller => "entry", :slug => entry.obj_name, :year => entry.added_time.year} ... will automatically create a link of type /year/slug Then, in your controller: def show @entry = Entry.find_by_obj_name(@params[''slug'']) ... end Replace obj_name with the name of the column that holds the slug. This is how I do it. I''m sure there are plenty of other ways to achieve the same. -- Regards, Stian Grytøyr
I have tried it like this: Somewhere in the ''list''-view: <%= link_to(''Show'', :action => ''show'', :id => @obj.name) %> In the controller: def show @obj = Object.find_by_name @params[''id''] end This gives me a link like /object/show/obj_name which leads to a 404. Do I have to change something in the .htaccess file? -- Nicky
I think the default .htaccess rules only match digits for the third parameter (to allow for module rewrites as well), so some tweakin'' will need to be done to the patterns and rules. Justin On 24/02/2005, at 7:49 PM, Nickolay Kolev wrote:> I have tried it like this: > > Somewhere in the ''list''-view: > > <%= link_to(''Show'', :action => ''show'', :id => @obj.name) %> > > In the controller: > > def show > @obj = Object.find_by_name @params[''id''] > end > > This gives me a link like /object/show/obj_name which leads to a 404. > Do I have to change something in the .htaccess file? > > -- Nicky > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >--- Justin French, Indent.com.au justin.french-zULN+VWqVOIpAS55Wn97og@public.gmane.org Web Application Development & Graphic Design
Nickolay Kolev schrieb:> I have tried it like this: > > Somewhere in the ''list''-view: > > <%= link_to(''Show'', :action => ''show'', :id => @obj.name) %> > > In the controller: > > def show > @obj = Object.find_by_name @params[''id''] > end > > This gives me a link like /object/show/obj_name which leads to a 404. Do > I have to change something in the .htaccess file?"Object" is a reserverd word. It is the base class of Ruby. I wouldn''t use it as a model name ;) Otherwise I don''t see why this shouldn''t work. Try hardcoding the values to test. Sascha
On Thursday 24 February 2005 05:33, Justin French wrote:> I think the default .htaccess rules only match digits for the third > parameter (to allow for module rewrites as well), so some tweakin'' will > need to be done to the patterns and rules.I should that Rails 0.10.0 can parse URLs with non-numeric id''s out of the box. -- Nicholas Seckar aka. Ulysses
Here''s my mod_rewrite rule that handles ID: RewriteRule ^([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)$ ?controller=$1&action=$2&id=$3 [QSA,L] I believe webrick uses a very similar rule. I''ve been passing string "slugs" around since I started railing. On Thu, 24 Feb 2005 10:02:08 -0500, Nicholas Seckar <nseckar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Thursday 24 February 2005 05:33, Justin French wrote: > > I think the default .htaccess rules only match digits for the third > > parameter (to allow for module rewrites as well), so some tweakin'' will > > need to be done to the patterns and rules. > > I should that Rails 0.10.0 can parse URLs with non-numeric id''s out of the > box. > > -- > > Nicholas Seckar aka. Ulysses > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- rick http://techno-weenie.net