SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extracting parts of a string

    In an AJAX auto complete I return strings like: (61) Ashdown Perri Anne 345 Pleasant Ln. The bracketted number is just the id number and is really the number that I want to extract. I am thinking of using a regular expression (whatevere it is - feel free to suggest one) to extract the number but perhaps ror has a better way of doing this or maybe there is even a better way of extracting the id number from the auto complete and putting it into a hidden field that gets passed with the post and is available in params. I am open to suggestions.

    Rick
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regex is the way to go:
    Code:
    irb(main):011:0> "(61) Ashdown Perri Anne 345 Pleasant Ln." =~ /\d+/
    => 1
    irb(main):012:0> $&
    => "61"
    the 1 on the second line indicates that there was a match. If there is no number, you can check with: if thestr =~ /\d+/.

    The "61" on the last line is the thing you want (convert to integer with String#to_i)

    But if you are using a dropdown box, isn't it possible to use ONLY the id as the value?

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may be possible but I don't know how to do it. It is an auto_complete box that only passes the model name and method(field) of what it is you are trying to look up. It returns the whole recordset abd makes the various fields available but it only posts the lookup field - at least as far as I can see or know (which is not far and not much).

    In retrospect though the auto_complete may be good where the choices are limited but in my db of 2.6m names there could be hundreds of Smiths. So I think I will revisit this problem and solve it as I have done in php. I will create a popup, do a search on a number of values (name, address, city, etc) and select the correct person fom the returned recordset. Since I only need the id number, I can return this value to the parent. I have seen a tabbed version of this somewhere - do you know of a tutorial that could put this process into steps using ror?

    Rick
    Ruby, Ruby when will you be mine

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I don't know.

    But you don't have to expose the id to your users, couldn't you just omit the id:

    "Ashdown Perri Anne 345 Pleasant Ln." only,

    and then select it like this:

    Address.find_by_y(params[:address])

    Where y is the column where this data comes from.


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
  •