2010-11-02 48 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