Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 关于Rails 3中嵌套路由的混淆,特别是自动生成的路径_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 关于Rails 3中嵌套路由的混淆,特别是自动生成的路径

Ruby on rails 关于Rails 3中嵌套路由的混淆,特别是自动生成的路径,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,对于每个物种,我都有很多数据集。对于每个数据集,我有许多表型。这些数据集的名称在一个物种中是主键。该物种也有字符串主键(例如,Hs代表智人)。因此,我希望能够指定一种表型,如下所示: /species/Hs/mcgary/1 其中,mcgary是表型集的名称(slug) 我知道,我可以通过在routes.rb文件中放入以下行来获得这种结果: match "/species/:species_id/:dataset(/:id/:action)" => 'phenotypes' match

对于每个物种,我都有很多数据集。对于每个数据集,我有许多表型。这些数据集的名称在一个物种中是主键。该物种也有字符串主键(例如,Hs代表智人)。因此,我希望能够指定一种表型,如下所示:

/species/Hs/mcgary/1
其中,mcgary是表型集的名称(slug)

我知道,我可以通过在routes.rb文件中放入以下行来获得这种结果:

match "/species/:species_id/:dataset(/:id/:action)" => 'phenotypes'
match "/species/:species_id/:dataset/:id" => 'phenotypes#show'
表型是表型控制器。物种有一个控制器,但数据集没有——它的功能由物种和表型的控制器处理。)

不幸的是,这并不能保证这些路径会起作用,例如,
编辑\物种\数据集\表型\路径
。我不太确定如何在routes.rb中编写该指令。除匹配说明外,一种可能性是具有以下内容:

resources :species do
  resources :dataset do
    resources :phenotypes
  end
end
然后设置一个重定向。但这很尴尬。有什么方法可以使用匹配符号使路径工作吗?喜欢新路线,但希望文档中有一些完整的示例

我还注意到,如果我执行类似于编辑物种数据集路径(物种,数据集)的操作,我可以获得
/species/:species\u id/:epotype\u set\u id
路由格式——但我不确定如何让它改为在物种上使用:abbrev,而不是每次键入species.abbrev。有没有办法告诉它在默认情况下使用该列而不是id


非常感谢。(是的,我意识到这样的嵌套路由会变得很尴尬。我同意。)

我找到了一个不完美的解决方案,那就是
资源()上的
:path
选项

这使我在我想要的路线上有了一点变化,三个控制器而不是两个,这并不理想,但重要的是,它可以工作

我的路径现在的形式是/species/Hs/datasets/mcgary/1(对于表型1)

我还必须在
ApplicationHelper
中编写一些helper方法。这使得拥有三重嵌套的资源更容易一些

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
不幸的是,这些路径有时似乎与自动生成的路径冲突。我不确定哪个模块拥有这些路径,因此很难重写它们


另一个问题是,我不太明白如何做物种路径(species),并让它使用缩写。相反,我必须做物种路径(species.abbrev)。

我发现了一个不完美的解决方案,那就是
资源()上的
:path
选项

这使我在我想要的路线上有了一点变化,三个控制器而不是两个,这并不理想,但重要的是,它可以工作

我的路径现在的形式是/species/Hs/datasets/mcgary/1(对于表型1)

我还必须在
ApplicationHelper
中编写一些helper方法。这使得拥有三重嵌套的资源更容易一些

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
不幸的是,这些路径有时似乎与自动生成的路径冲突。我不确定哪个模块拥有这些路径,因此很难重写它们

另一个问题是,我不太明白如何做物种路径(species),并让它使用缩写。相反,我必须做物种路径(species.abbrev)