如何在資源URL中使用連字符? 例如:/my-model/
或/my-model/1
。 如果我將路由定義爲resources :"my-model"
,則得到語法錯誤,因爲rails會生成方法def hash_for_my-models_url(options = nil)
。軌道3中的連字符資源路線
回答
我已經找到了解決方案:
resources "my-models", :as => :my_models, :controller => :my_models
更新: 正如Timo Saloranta在評論中所說,它在最新的Rails 3版本中沒有使用:controller => :my_models
。
我認爲這也行得通(不同之處在於路由助手的命名):'資源:產品類型,:路徑=>'/產品類型' – sandstrom 2011-10-17 18:49:06
它沒有**控制器* *部分最新的Rails 3版本。 – 2013-03-27 14:38:13
您是否嘗試過自定義路線?
map.connect "/my-model/:id", :controller => 'my-model-controller', :action => 'read'
這將調用「我的模型,controller.rb」的「讀」的方法。
是的,這是顯而易見的解決方案,但有趣的是如何使用資源定義連字符路線。 – 2010-11-02 15:55:09
可以使用:as
選項用連字符的URL配置足智多謀路線:
map.resources :my_model, :as => "my-model"
結果
my_model_index GET /my-model(.:format) {:action=>"index",
:controller=>"my_model"}
...等...
我已經這麼做了。當我使用連字符命名模型時,我得到了同樣的錯誤。 – 2010-11-03 02:08:02
即使您使用引用的字符串而不是符號? – zetetic 2010-11-03 06:21:16
是的......這是我第一次感謝.. – 2010-11-08 02:30:33
- 1. 連字符在軌道3路線
- 2. 資源路線和軌道
- 3. 在軌道上設置資源路線的命名路線
- 4. 軌道中的淺層路由嵌套的資源3
- 5. 軌道路由中的資源和資源之間的區別?
- 6. 路線在軌道3語法從軌道2〜3
- 7. 如何禁用部分軌道3上的資源路徑?
- 8. :軌道3路線的任何選項
- 9. 複雜的軌道3嵌套路線
- 10. 奇怪的軌道3路線問題
- 11. 軌道4 - 資源ID路線明確的型號名稱
- 12. 簡單的方法來規範軌道資源路線
- 13. 多個資源路由在軌道
- 14. 在軌複雜的嵌套路線/資源避免重複3
- 15. 軌道嵌套的資源和路由初始化資源
- 16. 軌道:在航線資源嵌套
- 17. 軌道3在資源航線附加參數約束
- 18. Rails 2路線到軌道3轉換
- 19. 導軌 - 自定義資源路線
- 20. 在軌資源刪除一條路線
- 21. 軌道3路線:不同的汽車路線模型
- 22. 資源vs資源導軌3
- 23. 通過軌道3中的查詢字符串進行路由?
- 24. 連字符在MVC 3條路線
- 25. 資源路線
- 26. Laravel路線:路線::資源
- 27. 嵌套資源與軌道
- 28. 動態軌道的路線
- 29. 軌道路線,只是沒有符號
- 30. 我如何覆蓋軌道3中的命名路線的範圍路線?
可能的重複[在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