Assume the method name is show_subjects −įinally your book_controller.rb file will look as follows − So, you can create a method inside book_controller.rb to display all the subjects. Additional Methods to Display SubjectsĪssume you want to give a facility to your users to browse all the books based on a given subject. The second line redirects the user to the list method using a redirect_to call. The first line finds the classified based on the parameter passed via the params object and then deletes it using the destroy method. If you want to delete a record from the database then you will use this method. Then = Subject.all line is required in case it does not save the data successfully, then it becomes similar to edit option. 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. Params.require(:book).permit(:title, :price, :subject_id, :description) If :action => 'show', :id => = Subject.all
Rails 5.1 new update#
The update method is similar to the create method and will be used to update existing books in the database. 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 second line grabs all the subjects from the database and puts them in an array called Implementing the update Method This method will be called to display data on the screen to be modified by the user. The only difference is 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. The edit method looks nearly identical to the show method. Then = Subject.all is required in case it does not save data successfully and it becomes similar case as with new option. 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. If it doesn't save, the user is sent back to the new method. The next line is a conditional statement that redirects the user to the list method if the object saves correctly to the database. The data was passed from the new method to create using the params object. The book_params method is used to collect all the fields from object :books. The first line creates a new instance variable called that holds a Book object built from the data, the user submitted. Params.require(:books).permit(:title, :price, :subject_id, :description) If you look at book_controller.rb, you will find it as follows − It creates a file called app/controllers/book_controller.rb This command accomplishes several tasks, of which the following are relevant here − This is a Rails paradigm that you should follow each time you create a controller. Notice that you are capitalizing Book and using the singular form. We will create just one controller here − The process for creating a controller is very easy, and it's similar to the process we've already used for creating a model.
It manages sessions, giving users the impression of an ongoing interaction with our applications. It manages helper modules, which extend the capabilities of the view templates without bulking up their code. It manages caching, which can give applications orders-of-magnitude performance boosts. It handles people-friendly URLs extremely well. It is responsible for routing external requests to internal actions. The controller is also a home to a number of important ancillary services. It coordinates the interaction between the user, the views, and the model.
The Rails controller is the logical center of your application.