2013-02-12 56 views
0

我的Rails應用程序有一個單一的CustomerSelectionController,有兩個動作:如何創建軌道控制器操作?

指數:其顯示形式,其中用戶可以輸入客戶信息和 選擇:它只是顯示一個靜態頁面。

class CustomerSelectionController < ApplicationController 
    def index 
    end 

    def select 
    end 
end 

我已經創建了我的routes.rb文件中的條目:

resources :customer_selection 

在索引視圖和形式如下:

<h1>Customer Selection</h1> 

<%= form_tag("customer_selection/select", :method => "get") do %> 
    <%= submit_tag("Select") %> 
<% end %> 

但是當我點擊在瀏覽器中選擇按鈕,我所得到的全部是:

未知動作

無法找到CustomerSelectionController的動作'show'

我不知道爲什麼它試圖執行一個名爲show的動作?我沒有在任何地方定義或引用一個。

+0

http://guides.rubyonrails.org/routing.html – gabrielhilal 2013-02-12 18:00:19

+0

您是否將customer_selection/select路由到該方法? – TheDude 2013-02-12 18:01:21

回答

1

我不知道爲什麼它試圖執行一個叫show的動作?我沒有在任何地方定義或引用一個。

是的,你有。這就是resources所做的。它定義了七個默認的RESTful路由:索引,顯示,新建,創建,編輯,更新和銷燬。當您路由到/customer_selection/select時,匹配的路由是「/ customer_action /:id」或「show」路由。 Rails實例化你的控制器並嘗試調用它的「show」動作,傳入一個「select」的ID。

如果你想除了那些添加路由,你需要明確地定義它,你也應該明確說明你想要的路線,如果你不希望所有七千萬:

resources :customer_selection, only: %w(index) do 
    collection { get :select } 
    # or 
    # get :select, on: :collection 
end 

由於你有這麼幾條路線,你也可以只定義它們不使用resources

get "/customer_selection" => "customer_selection#index" 
get "/customer_select/select" 

需要注意的是,在第二個途徑,"customer_select#select"是隱含的。在只有兩段的路由中,如果不指定控制器/操作,則Rails將默認爲「/:controller /:action」。

+0

然後,我可以放棄資源行,讓convention-over-configuration接管嗎? – spierepf 2013-02-12 18:03:58

+0

不,您仍然需要定義路線。儘管如此,你沒有*可以使用'resources',看到我更新的答案。 – meagar 2013-02-12 18:04:42

+0

好的,我可以在哪裏獲得該代碼塊的簡要說明? – spierepf 2013-02-12 18:07:51