A Rails View is an ERB program that shares data with controllers through mutually accessible variables. Rails uses ERB (Embedded Ruby) as its default template engine to interpret and process .html.erb files.
If you look in the app/views directory of any Rails application, you will see one subdirectory for each of the controllers. Each of these subdirectories was created automatically when the same-named controller was created with the generate script.
Rails lets you know that you need to create the view file for each new method. Each method you define in the controller needs to have a corresponding erb file, with the same name as the action (method), to display the data that the method is collecting.
Assume that a controller is created with the following command:
rails generate controller Book
This will create the controller (app/controllers/books_controller.rb). If you define an action inside this controller:
defindexend
You need to create the view file as app/views/books/index.html.erb.
ERB (Embedded Ruby) allows you to write Ruby code inside HTML using special tags:
ERB Syntax
Purpose
<% … %>
Executes Ruby code (no output)
<%= … %>
Executes and outputs the result
<%# … %>
Comment (ignored in output)
So, let’s create view files for all the methods we have defined in the books_controller.rb. While executing these views, simultaneously check these actions are applicable into the database or not.
Creating View File for create Action
Modify the books_controller.rb file to add following action methods:
defnew@book=Book.newend
Here, @book is an instance variable. The new action initializes a new Book and used in app/views/books/new.html.erb to build the form and enter Book details. (We shall see how to create the form).
Also define create action to handle the form submission. The form data is submitted to this action which calls save method of the book instance and pushes a flash message indicating the success.
flash[:notice]="Book was successfully created."
redirect_to @book# Redirects to show action (book details)else
render :new, status::unprocessable_entityendend</pre>
Rails form Helpers
Rails recommends using its helpers instead of using the normal HTML elements to build the content tags and form elements. The following code in new.html.erb uses form helpers like form_with, form.label, form.text_field etc. to render a blank Book form.
Following helpers are used inside the form helper form_with, for example −
<%= form_with(model: @book, local: true) do |f| %>
<%= f.label :title%>
<%= f.text_field :title %><%= f.submit "Save"%>
<% end %>
Form element helpers
label : renders a HTML label
text_field : renders a text input element, equivalent to <input type= "text">
text_area : generates a textarea element
radio_button : Rails helper for <input type= "radio">
check_box : used to render a check box, as done by <input type="checkbox">
submit : outputs an <input type= "submit"> button that submits the form.
URL & Link Helpers
link_to : creates a hyperlink, equivalent to href
button_to : generates a form-based button
image_tag : Inserts an image, similar to img src
Make sure that the config/routes.rb file is present with following resources statement.
resources :books
Start the Rails server
rails server
Add New Book
Visit http://localhost:3000/books/new URL to display the form.
Enter data for a new book and click the Create Book button.
Creating View File for list of books
The index action in the BooksController stores all the objects in the Book model in @books variable.
defindex@books=Book.all
end
This will need the index.html.erb file. The book data is displayed in a HTML table. In addition to the columns title, author and price, we use link_to helper to hyperlink buttons for show, edit and delete actions are provided in each row.
We also use the flash[:notice] helper to extract the flash messages pushed by any of the earlier actions.
While on the index page that shows the list of books, click on the edit button to update the details. Change the desired data and click on Update book button. The browser returns the list of books with a flash message âBook was successfully Updatedâ
Finally, the delete action in the BooksController calls the destroy() method on the selected Book object and removes from the book model.
Leave a Reply