views:

506

answers:

3

What do you recommend for displaying URLs and handling views in Rails for cities, regions, countries?

i.e:

/us/ca/San-Fransisco

/countries/us/regions/ca/cities/San-Fransisco

Should be able to: List all countries, list all regions within country and list all cities within a region and country.

Where would you put code for displaying all regions in a country?:

/us/regions/

/us

Should the code be in the region (action show) or country (action index) controller?

+1  A: 

I would prefer the simple /us/ca/San-Fransisco. A shorter path will result in a listing:

  • / lists countries
  • /us lists us states
  • /us/ca lists californian cities

Simple, short, clean - and will be honored by google ;-)

Arne Burmeister
+2  A: 

Agree with Arne. I don't see any problem with the first variant.

As for the second question, you better get the list of regions in the RegionsController.

Say you want to

GET /us/regions

then you should route it to

class RegionsController < ApplicationController
  # GET /:country_code/regions
  # GET /regions
  def index
    @countries = 
      if params[:country_code]
        Country.find_by_code(params[:country_code]).regions
      else
        Region.all # get all regions if country_code is not set
      end
  end
end
Milan Novota
but that is not the url that Arne says: /us/ which would list regions. In your case, what would /us/ output?
Espen
Oh. I didn't notice. I was a bit sleepy, sorry. Nonetheless, you need one URL that will identify a country and one URL to identify a collection of its states. And here, you are free to decide. You can make /us to be the URL of the country or the URL of the regions(states).
Milan Novota
If you'll go for the first option, you'll need one extra URL for collection of regions (that's what I did).In the second case, you'd need one extra URL for the country (which could be eg. /countries/us). The URL for region stays the same - eg. /us/ca. So, it's up to you which one you prefer.
Milan Novota
A: 

Hi, this is really interesting. I'm a newbie in website programming. I was wondering from where I can get the list of regions or cities in a specific country. What webservice should I use or should I store all the list in my own database? If option 2 is recommended, from where should I get this list? Any help is very appreciated.

Thanks :)

I blogged about it: http://blog.inspired.no/populate-your-database-with-free-world-cities-countries-regions-in-2-minutes-using-a-rails-migration-273
Espen