1

我正在使用contact_us gem 0.5.4版

我的 routes.rb 文件中有以下代码

resources :contacts, controller: 'contact_us', only: [:new, :create] do
  root :to => 'contact_us#new'
end

据我了解,上述路线contacts仅支持:new:create操作,并且使用指定的控制器controller: 'contact_us'也可以使用 root/重定向到#new操作,但是当我在浏览器中点击http://localhost:3000/contact-us时,它会说

未知操作
找不到 ContactUsController 的操作“索引”

我已将 rails 版本从 3.2.19 升级到 4.0.13,将 ruby​​ 升级到 2.0.0p481

旧代码在 rails 3.2.19 和 ruby​​ 1.8.7 上运行良好

resources :contacts,
  :controller => 'contact_us',
  :only       => [:new, :create]
match 'contact_us' => 'contact_us#new'

如果我只在上面的代码中进行更改matchget则会引发此错误

/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:430:in `add_route':路由名称无效,已在使用中:'contact_us'(参数错误)

您可能已经使用该:as选项定义了两个具有相同名称的路由,或者您可能正在覆盖已由具有相同名称的资源定义的路由。对于后者,您可以限制使用创建的路由,resources如下所述:

4

3 回答 3

3

添加:as路线可以完成工作

resources :contacts,
  :controller => 'contact_us',
  :only       => [:new, :create]
get 'contact_us' => 'contact_us#new', as: :contact_us2

正如Albin在聊天中所标识的,contact_us 模块路由文件已经具有相同的路由但具有不同的别名

get "contact-us" => "contact_us/contacts#new", as: :contact_us #line#11

我只是添加了具有不同路径和不同别名的相同路线,

于 2016-11-17T14:28:33.427 回答
3

您可以像在 rails 3.2 中那样做,只需matchget. 不再允许匹配任何动词。

resources :contacts,
  :controller => 'contact_us',
  :only       => [:new, :create]
get 'contact_us' => 'contact_us#new'

编辑

我们在聊天中解决了这个问题。事实证明这是与 gem contanct_us的碰撞。

于 2016-11-17T13:00:35.910 回答
1

尝试这个

resources :contacts, controllers: 'contact_us', :only => [:new, :create]

root :to => 'contact_us#new'
# or without root 
match 'contact_us' => 'contact_us#new', via: [:get]
于 2016-11-17T13:19:15.120 回答