如何向已创建的控制器添加新操作?

时间:2022-12-01 22:43:45

I am using rails and i need to know how i add a new action to my controller, im a beginner in rails so i dont really know if i just type "def action" in the controller.rb or is their more too it then that?

我正在使用rails,我需要知道我如何向我的控制器添加一个新动作,我是一个初学者在rails中所以我真的不知道我是否只是在controller.rb中键入“def action”或者是他们更多它然后那个?

Thanks.

3 个解决方案

#1


Nope, that's it... simply adding a method to the controller class automatically creates a new action for that controller (at least with the default setup).

不,就是这样......简单地向控制器类添加一个方法会自动为该控制器创建一个新动作(至少使用默认设置)。

There's a little section about this in the Beginning Ruby on Rails book. Also, reading through the Ruby on Rails Getting Started Guide would probably be helpful. It demonstrates this.

在Beginning Ruby on Rails一书中有一些关于这个的部分。另外,阅读Ruby on Rails入门指南可能会有所帮助。它证明了这一点。

#2


Well, it actually depends on how your routing is set up. If it falls through to the default route:

嗯,这实际上取决于您的路由设置方式。如果它落到默认路由:

map.connect ':controller/:action/:id'

Then you have nothing else to do (see ActionController::Routing).

然后你没有别的事情要做(参见ActionController :: Routing)。

If you are using RESTful resources, you need to explicitly mention the action (and request type) in the routes.rb file (see ActionController::Resources).

如果您正在使用RESTful资源,则需要在routes.rb文件中明确提及操作(和请求类型)(请参阅ActionController :: Resources)。

#3


As well as defining a new method in your controller, you will probably need to setup the view as well. By default the view corresponds to the controller and action name.

除了在控制器中定义新方法之外,您可能还需要设置视图。默认情况下,视图对应于控制器和操作名称。

So controller main with action index looks for a template in:

所以带有动作索引的控制器主要在以下位置查找模板:

views/main/index

#1


Nope, that's it... simply adding a method to the controller class automatically creates a new action for that controller (at least with the default setup).

不,就是这样......简单地向控制器类添加一个方法会自动为该控制器创建一个新动作(至少使用默认设置)。

There's a little section about this in the Beginning Ruby on Rails book. Also, reading through the Ruby on Rails Getting Started Guide would probably be helpful. It demonstrates this.

在Beginning Ruby on Rails一书中有一些关于这个的部分。另外,阅读Ruby on Rails入门指南可能会有所帮助。它证明了这一点。

#2


Well, it actually depends on how your routing is set up. If it falls through to the default route:

嗯,这实际上取决于您的路由设置方式。如果它落到默认路由:

map.connect ':controller/:action/:id'

Then you have nothing else to do (see ActionController::Routing).

然后你没有别的事情要做(参见ActionController :: Routing)。

If you are using RESTful resources, you need to explicitly mention the action (and request type) in the routes.rb file (see ActionController::Resources).

如果您正在使用RESTful资源,则需要在routes.rb文件中明确提及操作(和请求类型)(请参阅ActionController :: Resources)。

#3


As well as defining a new method in your controller, you will probably need to setup the view as well. By default the view corresponds to the controller and action name.

除了在控制器中定义新方法之外,您可能还需要设置视图。默认情况下,视图对应于控制器和操作名称。

So controller main with action index looks for a template in:

所以带有动作索引的控制器主要在以下位置查找模板:

views/main/index