SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using a name as a param instead of an id

    I'm sure I've seen this question posted here before, but I could not find it.

    What I want to do is use a name in my url instead of an id, then use the .find method to get the correct record.

    Eg.

    URL = http://www.foobar.com/person/show/Bob

    In PersonController for the action 'show' I want to end up with SQL that looks like "SELECT * FROM people WHERE name = 'Bob'", but I want to just use the .find method to keep my code more readable. Right now I am trying this:

    @person = person.findall, :conditions => params["name"])

    I don't get an error, but @person is not defined in the view, so it's obviously not getting the record.

    Any help is appreciated.

    Thanks.

  2. #2
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you want to do is use something like:

    Code:
    @person = Person.find_by_name(@params[:name])

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will also need to make sure you have a route configured so that "bob" is recognised as a name in the url /person/show/bob:

    Code:
    map.connect 'person/show/:name', :controller => 'person', :action => 'show'

  4. #4
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks to both of you. I do remember reading about it now.

    Maybe this summer I will have time to start a community-driven Rails documentation web site. I'd love to have a resource that's as good as the PHP manual. Then again, by this summer maybe somebody else will have beaten me to the punch.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •