The Controller is an integral component of the MVC (Model/View/Controller) concept that is central to Rails. Controllers are normal Ruby classes that inherit from ActionController:Base or more frequently the generated ApplicationController Class
Controllers have public action methods that can be called from the dispatcher as well as their own protected and private methods. Typically, action methods follow this pattern :-
- Manipulate the domain model in some way
- Refers to the response format requested by the user
- Generate a response in the correct form by rendering a view template
script/generate controller Concepts
exists app/controllers/
exists app/helpers/
create app/views/concepts
exists test/functional/
exists test/unit/helpers/
create app/controllers/concepts_controller.rb
create test/functional/concepts_controller_test.rb
create app/helpers/concepts_helper.rb
create test/unit/helpers/concepts_helper_test.rb
As such, you should remove all Business Logic from your Controllers and put in the corresponding Model.
So what does the Controllers in your Rails application do ?
Well, your controllers are only responsible for mapping between URLs, co-ordinating with your Models and your Views and channeling back to a HTTP response. In addition, it may do Access Control as well.
Basically, when someone connects to your Rails Application via an URL, they are ly asking your Application to execute a Controller Action. Typically, action methods follow this pattern :-
- Interact with the domain model in some way such as selecting, inserting or updating data based on incoming parameters or using one or more classes form the app/model directory
- Generate a response by rendering a view template with the same name as the action method
As you may be aware, Rails works magically so long as you adhere to the all important concepl of Convention over Configuration. In this respect Controller Conventions include :-
a) The Controller's class name should be camel cased and pluralised and be followed by the word controller. eg.CustomersControllers or BillNosControllers
b) Similar to Models, the controller filename should be lowercased and underscored versions of the class name. eg. customers_controllers.rb or bill_nos_controllers.rb
c) An action is a Public Method of a controller class. For example
- index
- show
- new
- create
- edit
- update
- destroy
What is fascinating is that unless explicitly told to do , Rails will render the views with the same name as the actions!
How to remove a controller and all previously generated files ?
chee@ibm4linux:~/workspace/crm2009$ script/destroy controller concepts
rm test/unit/helpers/concepts_helper_test.rb
rm app/helpers/concepts_helper.rb
rm test/functional/concepts_controller_test.rb
rm app/controllers/concepts_controller.rb
notempty test/unit/helpers
notempty test/unit
notempty test
notempty test/functional
notempty test
rmdir app/views/concepts
notempty app/views
notempty app
notempty app/helpers
notempty app
notempty app/controllers
notempty app
chee@ibm4linux:~/workspace/crm2009$
No comments:
Post a Comment