Derek
Derek

Reputation: 853

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 views automatically to the controller?

If I try the same generate code (with different method names) again, it wants to override the existing controller.

Upvotes: 3

Views: 4877

Answers (1)

simonwh
simonwh

Reputation: 1007

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.

Upvotes: 7

Related Questions