2016-11-17 39 views
1

我使用contact_us寶石版本0.5.4Rails 4.0.x如何將contact_us gem的root動作路由到指定的動作?

我下面的代碼在我的routes.rb文件

resources :contacts, controller: 'contact_us', only: [:new, :create] do 
    root :to => 'contact_us#new' 
end 
在我的理解上面的路線 contacts將只支持 :new:create行動

,並與指定控制器controller: 'contact_us'還將其與根/會重定向到#new動作,但是當我在瀏覽器中點擊http://localhost:3000/contact-us它說

未知的動作
行動「索引」不能爲ContactUsController

發現我已經升級,從3.2.19軌道版本4.0.13和紅寶石以2.0.0p481

老代碼工作正常使用Rails 3.2.19和1.8.7紅寶石

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

如果我只用get在上面的代碼拋出這個錯誤更改match

/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」 (引發ArgumentError)

你可能已經使用:as選項定義的兩條路線具有相同名稱 ,否則你可能會改寫已經 由資源具有相同定義的路由命名。對於後者,可以 限制與resources創建的路線如下解釋:

回答

3

在路由添加:as做的工作

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

如聊天CONTACT_US模塊鑑定Albinroute file它已經擁有相同的路線,但用不同的別名

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

我只是添加了不同路徑和不同別名的相同路由,

3

你可以做同樣的方式像你一樣在Rails 3.2你只需要到match交換到get。不再允許匹配任何動詞。

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

編輯

我們在聊天解決了這個。事實證明,這是與寶石​​的碰撞。

+0

是的,我做到了,但它拋出了另一個錯誤,請檢查問題更新 –

+0

這是如此奇怪。我開始了一個新的小測試項目。它工作順利... – Albin

+0

是你的整個路線文件? – Albin

2

試試這個

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

root :to => 'contact_us#new' 
# or without root 
match 'contact_us' => 'contact_us#new', via: [:get] 
+0

請檢查更新問題 –

+0

@ riksof-zeeshan:您是否嘗試過以上代碼? –

+0

是的,我做了'match'contact_us'=>'contact_us#new',通過:[:get]'並且它拋出這個錯誤無效的路由名稱,已經在使用:'contact_us' –

相關問題