2013-02-12 47 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