SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Zurich, Switzerland
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    link_to question

    I have no problem with link_to calling either a single record or all records. But suppose I have a table of 130 countries and I just want North America (eg 'Canada,Mexico,US'). I can write a separate list function and view for each continent, but is there a way to just pass the relevant countries as an array to list using link_to?

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can pass arbitrary params to link_to:

    Code:
    link_to 'text for the link', :action => 'xyz', :foo => 'bar'
    Now you can do this in the controller it points to:

    Code:
    def xyz
      params[:foo] # this is 'bar'
    end
    So you could do:

    Code:
    link_to 'text', :action => 'list_some', :countries => @countries.map{|country|country.id}.join(',')
    If @countries is an array of the countries you want to link to this creates a link of this form:

    /controller/list_some?countries=23,32,54,...other id's

    Then you do this in the controller:

    Code:
    def list_some
      country_ids = params[:country].split(',')
      ...
    end
    But this is rather messy. Are you sure that you don't want a separate page for each country? Maybe you could put all information in the list view (and possibly hide it with Javascript, then create a link to show it again)?
    Last edited by Fenrir2; Feb 6, 2007 at 12:51.

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Why not create a table of continents, make countries belong_to a continent, and search by continent ID instead? Not nearly as messy as sticking a bunch of country codes in a comma-separated list in the querystring.

  4. #4
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Zurich, Switzerland
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the current version, (procedural sql), each continent is defined as a string variable with the different country names in quotes and the sql would read something like:

    select * from countrysummaries where name in ('Canada','Mexico','US')

    Obviously the list is substantially longer for Asia, Europe, Africa, etc.

    the link reads something like source=countrysummaries&action=show&country=NorthAmerica

    As the procedural code validates the country parameter, it realizes that the country is special, and drops in the continent string which includes all the relevant countries rather than just the normal single country name.

    I like the belong_to continent idea, it seems more "rails like".


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
  •