在rails中映射操作

时间:2012-03-02 14:19:59

标签: ruby-on-rails routing

在我的routs.rb中,我有以下几行

CyberTrackRails3::Application.routes.draw do
  scope "(:locale)", :locale => /en|de|nl/ do
    resources :transfusion do
      get 'detail', :on => :collection
    end
  end
end

下一行

url_for( :controller => :transfusion, :action => :detail, 
            :id => bloodselection[:id] 

被映射到 / nl / transfusion / detail?id = 162106

如何将其映射到 / nl / transfusion / 162106 ,以便控制器将其理解为ID为162106?

2 个答案:

答案 0 :(得分:2)

这是因为您在集合上设置详细操作,要遵循您的网址结构,需要在成员上进行定义。

CyberTrackRails3::Application.routes.draw do
  scope "(:locale)", :locale => /en|de|nl/ do
    resources :transfusion do
      get 'detail', :on => :member
    end
  end
end

答案 1 :(得分:1)

Rails路由引擎默认情况下会使集合多元化,因此如果您希望默认设置能够开箱即用,则应使用复数transfusions

然后,您可以使用url_for代替transfusion_path获取/nl/transfusions/12345

如果您坚持在URL中使用单数形式,则可以将路径段覆盖为

resources :transfusions, path: "transfusion" do …

可让您获得/nl/transfusion/12345