2010-11-02 175 views
10

如何在資源URL中使用連字符? 例如:/my-model//my-model/1。 如果我將路由定義爲resources :"my-model",則得到語法錯誤,因爲rails會生成方法def hash_for_my-models_url(options = nil)軌道3中的連字符資源路線

+0

可能的重複[在Ruby on Rails路由我想使用Dash \' - \'而不是Underscore \'\ _ \'](http://stackoverflow.com/questions/5334465/in-ruby- on-rails-routing-i-like-like-use-dash-instead-of-underscore) – 2015-04-03 21:26:32

回答

14

我已經找到了解決方案:

resources "my-models", :as => :my_models, :controller => :my_models 

更新: 正如Timo Saloranta在評論中所說,它在最新的Rails 3版本中沒有使用:controller => :my_models

+1

我認爲這也行得通(不同之處在於路由助手的命名):'資源:產品類型,:路徑=>'/產品類型' – sandstrom 2011-10-17 18:49:06

+3

它沒有**控制器* *部分最新的Rails 3版本。 – 2013-03-27 14:38:13

0

您是否嘗試過自定義路線?

map.connect "/my-model/:id", :controller => 'my-model-controller', :action => 'read' 

這將調用「我的模型,controller.rb」的「讀」的方法。

+0

是的,這是顯而易見的解決方案,但有趣的是如何使用資源定義連字符路線。 – 2010-11-02 15:55:09

2

可以使用:as選項用連字符的URL配置足智多謀路線:

map.resources :my_model, :as => "my-model" 

結果

my_model_index GET /my-model(.:format) {:action=>"index", 
    :controller=>"my_model"} 

...等...

+0

我已經這麼做了。當我使用連字符命名模型時,我得到了同樣的錯誤。 – 2010-11-03 02:08:02

+0

即使您使用引用的字符串而不是符號? – zetetic 2010-11-03 06:21:16

+0

是的......這是我第一次感謝.. – 2010-11-08 02:30:33