Ruby-on-rails 如何向现有控制器添加新操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18703349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a new action to the existing controller?
提问by Calirails
I'm pretty new in Rails. Sorry for the noob question.
我是 Rails 的新手。对不起,菜鸟问题。
I've create a new controller: rails new controller Say hello goodbye
我创建了一个新控制器: rails new controller Say hello goodbye
How can i add a new action like "hello" and "goodbye" to this existing controller?
如何向现有控制器添加“你好”和“再见”等新动作?
回答by fotanus
Add a new action is simple. All you have to do is add a method on your controller, like, for example:
添加新动作很简单。您所要做的就是在控制器上添加一个方法,例如:
# app/controllers/dummy_controller.rb
def get_back
logger.warn "It works!"
redirect_to :back
end
Now, to be able to access this action throgh a URL, you need to have a route for that. This is done in your config/routes.rbfile. You can add it as a hard route, like
现在,为了能够通过 URL 访问此操作,您需要为此设置一个路由。这是在您的config/routes.rb文件中完成的。您可以将其添加为硬路线,例如
get '/go_back', to: "dummy#get_back"
This is the simplest possible route. But you might want it to behave like a restful route. This is useful if you are doing an action over one or more models. So your in your route file, you will have something like this:
这是最简单的可能路线。但您可能希望它表现得像一条宁静的路线。如果您对一个或多个模型执行操作,这将非常有用。所以你在你的路由文件中,你会有这样的东西:
resources :dummy do
collection do
get 'get_back'
end
end
This allows you to accept a getmethod over a collection. You will have the helper dummy_go_back_url, and to get to this page the url is /dummies/go_back.
这允许您get通过集合接受方法。您将拥有帮助程序dummy_go_back_url,并且要访问此页面,网址为/dummies/go_back。
This is for acting over a collection of resources. If you are acting on one specific object, you should specify a memberaction:
这是用于对资源集合进行操作。如果您正在对一个特定对象执行操作,则应指定一个member操作:
resources :dummy do
member do
get 'get_back'
end
end
Since a member action is for only one object, you will have a url like /dummies/123/go_back. This automatically will set the variable params[:id]in your controller to 123, allowing you to easily fetch your object. Also, the helper method dummy_go_back_pathis defined, and received one object or id as parameter to generate the correct url.
由于成员操作仅适用于一个对象,因此您将拥有类似/dummies/123/go_back. 这会自动将params[:id]控制器中的变量设置为123,让您轻松获取对象。此外,helper 方法dummy_go_back_path已定义,并接收一个对象或 id 作为参数以生成正确的 url。
These are the most simple routes you can have, but you can look in routing outside infrom rails guides as a reliable source of information.
这些是您可以拥有的最简单的路线,但您可以将导轨从外向内的路线视为可靠的信息来源。
回答by Optimus Pette
def hello
@hello = "hello"
end
def goodbye
@goodbye = "goodbye"
end
then in /config/routes.rb
然后在 /config/routes.rb
get 'foo/hello' ## foo is the name of your controller
get 'foo/goodbye'
Remember to create the views too:
views/foo/hello.html.erbthat may look like this:
请记住也创建视图:
views/foo/hello.html.erb可能如下所示:
Say <%= @hello %>
views/foo/goodbye.html.erbthat may look like this:
views/foo/goodbye.html.erb可能看起来像这样:
Say <%= @goodbye %>
回答by rexxar
When you repeat the generatecommand with another method's name, you can skip overwriting the existing controller and it's test implementation. That will automatically add the route and create a view. After that you have to add the method manually to the controller because that action didn't touch the controller file.
当您generate使用另一个方法的名称重复该命令时,您可以跳过覆盖现有控制器及其测试实现。这将自动添加路线并创建视图。之后,您必须手动将方法添加到控制器,因为该操作没有触及控制器文件。
Example:
例子:
one method called newwas already created in the controller when it was initially created:
一个被调用的方法new在最初创建时已经在控制器中创建了:
$ rails generate controller Person new
... successfully created the controller, it's route and view ...
when trying to add a new method:
尝试添加新方法时:
$ rails generate controller Person all
Running via Spring preloader in process 28648
conflict app/controllers/person_controller.rb
Overwrite ../app/controllers/person_controller.rb? (enter "h" for help) [Ynaqdh] h
Y - yes, overwrite
n - no, do not overwrite
a - all, overwrite this and all others
q - quit, abort
d - diff, show the differences between the old and the new
h - help, show this help
Overwrite ../app/controllers/person_controller.rb? (enter "h" for help) [Ynaqdh] n
skip app/controllers/person_controller.rb
route get 'person/all'
invoke erb
exist app/views/person
create app/views/person/all.html.erb
invoke test_unit
conflict test/controllers/person_controller_test.rb
Overwrite ../test/controllers/person_controller_test.rb? (enter "h" for help) [Ynaqdh] n
skip test/controllers/person_controller_test.rb
invoke helper
identical app/helpers/person_helper.rb
invoke test_unit
invoke assets
invoke coffee
identical app/assets/javascripts/person.coffee
invoke scss
identical app/assets/stylesheets/person.scss
回答by BroiSatse
You simply open controller file (in app/controllers/) and define new methods there. However, if you generated this controller (say_controller) the way you wrote, those two actions should already be there.
您只需打开控制器文件(在 app/controllers/ 中)并在那里定义新方法。但是,如果您按照您编写的方式生成了这个控制器 (say_controller),那么这两个操作应该已经存在了。

