New to Rails: How to Add New Functions to a Controller that Already Exists?
One uses
rails generate controller ControllName function1 function2 etc
to generator a controller and functions with views for each function. Once the controller already exists, though, how would I use a similar command to add more functions and view开发者_开发知识库s automatically to the controller?
If I try the same generate code (with different method names) again, it wants to override the existing controller.
The generators, as already mentioned in a comment, is just for getting started. If you want to add a new action (method/function), just go ahead and define it.
def my_action
@things = Thing.all
...
end
Remember to map the new action in the config/routes.rb file. For example:
match '/my_action' => 'controllername#my_action', :as => 'my_action'
This will also give you the named routes my_action_path
and my_action_url
.
精彩评论