Ruby on Rails Rails Controllers - Ruby on Rails

What is Rails Controller?

Now that we have our database and required tables available, let us see how to put a life in your application by writing Controller Code. The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model as well as houses a number of important ancillary services.

  • It is responsible for routing external requests to internal actions. It handles people-friendly URLs extremely well.

  • It manages caching, which can give applications orders-of-magnitude performance boosts.

  • It manages helper modules, which extend the capabilities of the view templates without bulking up their code.

  • It manages sessions, giving users the impression of an ongoing interaction with our applications.


What is the process of creating a Rails Controller?

Remember the process we have used for creating a model? The process for creating a controller is pretty similar and very easy. We will create just one controller here −

Note that you are capitalizing Book and using the singular form. This is a Rails paradigm that you should follow each time you create a controller.

This command accomplishes several tasks, of which the following are relevant here −

  • It creates a file called app/controllers/book_controller.rb


If you look at book_controller.rb, you will find it as follows −

Controller classes inherit from ApplicationController, which is the other file in the controllers folder: application.rb.

The ApplicationController contains code that can be run in all your controllers and it inherits from Rails ActionController::Base class.

Let's just define a few method stubs in book_controller.rb. Based on your requirement, you could define any number of functions in this file.

Modify the file to look like the following and save your changes. Give any relevant name to these methods.

Next let us see how to implement all the methods one by one.

How to implement the list Method

The list method provides a list of all the books in the database. This functionality will be achieved by the following lines of code, editing the following lines in book_controller.rb file.

The @books = Book.all line in the list method tells Rails to search the books table and store each row it finds in the @books instance object.

How to implement the show Method

Unlike the list method, the show method displays only further details on a single book. This functionality will be achieved by using

The show method's @book = Book.find(params[:id]) line tells Rails to find only the book that has the id defined in params[:id].

The params object is a container that enables you to pass values between method calls. For example, when you're on the page called by the list method, you can click a link for a specific book, and it passes the id of that book via the params object so that show can find the specific book.

How to implement the new Method

The new method lets Rails know that you will create a new object. So just add the following code in this method.

The above method will be called when you will display a page to the user to take user input. Here second line grabs all the subjects from the database and puts them in an array called @subjects.

How to implement the create Method

Once you take user input using HTML form, it is time to create a record into the database. To achieve this, edit the create method in the book_controller.rb to match the following −

The first line creates a new instance variable called @book that holds a Book object built from the data, the user submitted. The book_params method is used to collect all the fields from object :books. The data was passed from the new method to create using the params object.

The next line is a conditional statement that redirects the user to the list method if the object saves correctly to the database. If it doesn't save, the user is sent back to the new method. The redirect_to method is similar to performing a meta refresh on a web page: it automatically forwards you to your destination without any user interaction.

Then @subjects = Subject.all is required in case it does not save data successfully and it becomes similar case as with new option.

How to implement the edit Method

This method is similar to the show method, except that the show method is not editable. Both methods are used to retrieve a single object based on its id and display it on a page.

This method will be called to display data on the screen to be modified by the user. The second line grabs all the subjects from the database and puts them in an array called @subjects.

How to implement the update Method

This method will be called after the edit method, when the user modifies a data and wants to update the changes into the database. The update method is similar to the create method and will be used to update existing books in the database.

The update_attributes method is similar to the save method used by create but instead of creating a new row in the database, it overwrites the attributes of the existing row.

Then @subjects = Subject.all line is required in case it does not save the data successfully, then it becomes similar to edit option.

How to iimplement the delete Method

This method is used when you want to delete a record from the database. It is done as

The first line finds the classified based on the parameter passed via the params object and then deletes it using the destroy method. The second line redirects the user to the list method using a redirect_to call.

What is the additional method to display subjects?

Assume you want to give a facility to your users to browse all the books based on a given subject. So, you can create a method inside book_controller.rb to display all the subjects. Assume the method name is show_subjects

This is how your book_controller.rb file will look finally −

Now save your controller file.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails Topics