Partials

Hello Rails enthusiasts, We are currently at the halfway point of the bootcamp and concluding our lessons in Rails. As we endeavor to write DRY and readable code, I found a Rails capability that makes our views easier to read and less repetitive. — — Upon completing my most recent CRUD app, I noticed that my new and edit view pages were almost identical with the exception of a few lines of code. We can make these pages much more succinct if we reference a partial view page. Here’s how:

  1. Create a view page in the same view directory as the edit and new view pages.

  2. Name the file starting with and underscore. (example: _form.html.erb)

  3. Enter the html code that is to be utilized in the other view pages.

partial

  1. Use the partial in your view pages with this syntax:
    <%= render "form", header:"Edit Game",url: "/@game.id">product/#{@game.id}", method_var: :patch %>
    

    Notice that you will need a beehive tag in order to render your partial as you will be using ruby to reference the file.

You will also notice that I have placed variables within my partial since the form tags for my view page and my new page are different. (html header, url, and method).

Now, where there used to be 50+ lines of repetitive code in each file, there is one line of code referring to the partial with the same result!


To learn more about form tags click here

Sample to my CRUD APP click here


© 2017. All rights reserved.

Powered by Hydejack v7.4.2