views:

15

answers:

1

For each species, I have many datasets. For each dataset, I have many phenotypes. The datasets have names which, within a species, are primary keys. The species, too, have string primary keys (e.g., Hs for Homo sapiens). So, I want to be able to specify a phenotype as follows:

/species/Hs/mcgary/1

where mcgary is the name (slug) of the phenotype set.

I understand that I can get this kind of result by putting the following lines in my routes.rb file:

match "/species/:species_id/:dataset(/:id/:action)" => 'phenotypes'
match "/species/:species_id/:dataset/:id" => 'phenotypes#show'

Phenotypes is the phenotype controller. Species has a controller, but Dataset does not -- its functions are handled by Species' and Phenotype's controllers.)

Unfortunately, that doesn't guarantee that the paths will work, e.g., edit_species_dataset_phenotype_path. I'm not quite sure how to write that instruction in routes.rb. One possibility is to have, in addition to the match instructions, the following:

resources :species do
  resources :dataset do
    resources :phenotypes
  end
end

and just set up a redirect. But that's awkward. Is there some way I can use the match notation to get the paths working? Love the new routes, but wish the documentation had some full examples in it.

I also notice that if I do something like edit_species_dataset_path(species, dataset), I can get the /species/:species_id/:phenotype_set_id route format -- but I'm not sure how to get it to use :abbrev on Species instead, other than to type species.abbrev every time. Is there a way to tell it to use that column by default, instead of id?

Many thanks. (And yes, I realize nested routes like this get awkward. I'm okay with that.)

A: 

I found an imperfect solution, which is the :path option on resources().

resources :species do
  resources :datasets do
    resources :phenotypes, :path => ""
  end
end

This gives me a slight variation on the routes I had wanted, and three controllers instead of two, which is not ideal -- but importantly, it works.

My paths are now of the form /species/Hs/datasets/mcgary/1 (for phenotype 1).

I also had to write some helper methods in ApplicationHelper. These make it a bit easier to have triply-nested resources.

def phenotype_path(phenotype, dataset=nil, species=nil)
  dataset ||= phenotype.dataset
  species ||= phenotype.species
  File.join(phenotypes_path(dataset, species), phenotype.id.to_s)
end

def phenotypes_path(dataset, species=nil)
  species ||= dataset.species
  File.join(species_path(species.abbrev), "datasets", dataset.name)
end

def edit_phenotype_path(phenotype, dataset=nil, species=nil)
  File.join(phenotype_path(phenotype,dataset,species), "edit")
end

def new_phenotype_path(dataset, species=nil)
  File.join(phenotypes_path(dataset, species), "new")
end

alias :dataset_path :phenotypes_path

def edit_dataset_path(dataset, species=nil)
  File.join(dataset_path(dataset, species), "edit")
end

def dataset_path(dataset, species=nil)
  species ||= dataset.species
  File.join(species_path(species.abbrev), "datasets", dataset.name)
end

def datasets_path(species)
  species_datasets_path(species.abbrev)
end

Unfortunately, these paths sometimes seem to conflict with the autogenerated paths. I'm not sure which module holds those paths, so it's difficult to rewrite them.

The other issue is that I can't quite figure out how to do species_path(species) and have it use the abbreviation. Instead, I have to do species_path(species.abbrev).

mohawkjohn