What do you think will happen if you script/generate controller ?
Why wonder, just do it :-)
If you are using Ubuntu 9.04, just open a Terminal Window and cd to the root of your Rails applications and type script/generate controller -- as follows :-
chee@ibm4linux:~/workspace/crm2009$ script/generate controller
After pressing Enter, this is what you will see.
Usage: script/generate controller ControllerName [options]
Rails Info:
-v, --version Show the Rails version number and quit.
-h, --help Show this help message and quit.
General Options:
-p, --pretend Run but do not make any changes.
-f, --force Overwrite files that already exist.
-s, --skip Skip files that already exist.
-q, --quiet Suppress normal output.
-t, --backtrace Debugging: show backtrace on errors.
-c, --svn Modify files with subversion. (Note: svn must be in path)
-g, --git Modify files with git. (Note: git must be in path)
Description:
Stubs out a new controller and its views. Pass the controller name, either
CamelCased or under_scored, and a list of views as arguments.
To create a controller within a module, specify the controller name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers, view templates in
app/views/controller_name, a helper class in app/helpers, a functional
test suite in test/functional and a helper test suite in test/unit/helpers.
Example:
`./script/generate controller CreditCard open debit credit close`
Credit card controller with URLs like /credit_card/debit.
Controller: app/controllers/credit_card_controller.rb
Functional Test: test/functional/credit_card_controller_test.rb
Views: app/views/credit_card/debit.html.erb [...]
Helper: app/helpers/credit_card_helper.rb
Helper Test: test/unit/helpers/credit_card_helper_test.rb
Modules Example:
`./script/generate controller 'admin/credit_card' suspend late_fee`
Credit card admin controller with URLs /admin/credit_card/suspend.
Controller: app/controllers/admin/credit_card_controller.rb
Functional Test: test/functional/admin/credit_card_controller_test.rb
Views: app/views/admin/credit_card/debit.html.erb [...]
Helper: app/helpers/admin/credit_card_helper.rb
Helper Test: test/unit/helpers/admin/credit_card_helper_test.rb
Chee Chong Hwa aka CCH, a Malaysian Chief Software Architect blogs on his experiences with Ruby on Rail running on Win2003 Server+Apache 2.26 environment since 2007. Flirted briefly with Ubuntu 9.04 in 2009. Since January 2017, now using RubyMine IDE/Notepad++ to work with Ruby 2.26 +Rails 5.01+Apache 2.4.23 + MySQL 5.7.14 on Windows 10 64-bits.
Showing posts with label Controllers. Show all posts
Showing posts with label Controllers. Show all posts
Sunday, August 30, 2009
Friday, August 28, 2009
Rails Basics : What is a Controller ?
What is a Controller ?
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 :-
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 :-
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
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$
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$
Subscribe to:
Posts (Atom)
Welcome to Rails.. Rails... Rails !
In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.
As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam
CCH
10th June 2010, 19:42
As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam
CCH
10th June 2010, 19:42