Generate controller and actions in Rails

Rails controller generation is done with the help of Rake command line tool. Actions are added along with the controller. Action can be added directly into controller .rb file after initial controller generation. Here is a command line tool that does just that.

First example generates empty controller

rails generate controller Product

Second example generate controller with two three actions: index, show, edit

>rails generate controller Products index

Both commands will add the following files to our Rails application

        Controller: app/controllers/products_controller.rb
        Test:       test/controllers/products_controller_test.rb
        Views:      app/views/products/index.html.erb
        Helper:     app/helpers/products_helper.rb

Note: it is important to name your controllers as plural in order for internal plumbing to work properly.

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…