On Nov 20, 2007 9:46 AM, Infinit [email protected] wrote:
with exactly the same title string.
Thanks in advance.
Infinit
This is done by using a custom named route, as designated in your
config/routes.rb file.
A good way of explaining how this particular example
(http://blogs.tech-recipes.com/johnny/2006/08/27/ruby-on-rails-using-full-text-search-with-tagging)
may have been formed using Rails, would be this (a possible line in
the routes.rb file):
map.post ‘:user/:year/:month/:day/:title’, :controller => ‘posts’,
:action => ‘show’
This will route any request that matches that format (i.e.
/johnny/2006/08/27/ruby-on-rails-blah-blah) to the ‘show’ action under
your ‘posts’ controller. The values you specify as each ‘segment’ of
the uri (in this example, you are using ‘johnny’ under the :user and
2006 as the :year), will be passed to your controller under the
‘params’ hash, allowing you to access them from within the controller.
An example in your controller would be:
@post = Post.find(:first, :conditions => [:author => params[:user],
:subject => params[:title]])
[If anything I’m typing is wrong, please let me know everyone]. As you
can guess, this would allow you to pass the values in the (nicely
formed) URL to your controller, and work with it as you like.
In response to your second question; no, the title would not be the
primary key here. The primary key is actually not included in the URL
at all (unless you form a composite primary key, which would only seem
sensible to use all the segments together, but that’s probably not
very efficient), and you would have to write a find statement to
search your database for the first post that matches your input –
because of this, you run the risk of having two posts named exactly
the same posted on the same day, but only one would be found. If this
is the case, then a URL format like this obviously would not suffice.
You would have to add your own identifier on the end, perhaps:
map.post ‘:user/:year/:month/:day/:title/:iterator’, :controller =>
‘posts’, :action => ‘show’, :iterator => 1
(Note that the default value for your :iterator parameter can be set
in the routes file too, so if it is not set in the URL, it will
default to 1. You can also add validation on your params here, but
that is for a later e-mail =P).
This was, you can tell your ‘find’ statement to skip a certain number
of posts (from the same day, with the same name) in order to find the
one you want.
I hope this helps and I’m not completely missing the point of your
question. Also, if any rails ninjas superior to me spot any
inefficiencies or mistakes please call me out on it.
–
Edd Morgan
http://www.eddm.co.uk
+44 (0) 7805 089097