quinta-feira, 13 de abril de 2017

RAILS Faq HowTo Reference Sample Paginate index result and Lookup Table - Foreign Key

1. Introduction

This post gathers information about FAQ-Frequently Asked Question, How To, technical document references and samples about RAILS.

  • Paginating index result in Rails
  • Lookup Table, Foreign Key, 


2. FAQ, HOWTO, REFERENCES, SAMPLES, etc

2.1. Paginating in RAILS

The will_paginating gem modifies Collection of ActiveRecord to Display Page Links for Querying records. See also:

a) HOWTO


  • Step#1: add gem will_paginating
# vim Gemfile
  :
gem 'will_paginate', '~> 3.1.0'
  :


  • Step#2: bundle install
# bundle install


  • Step#3: Creating MVC for Product and custom controller for paginating each 5 records for page
# rails generate scaffold Product name price:decimal quantity:integer
# vim app/controllers/products_controller.rb
class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  def index
     @products = Product.all.paginate(page: params[:page], per_page: 5)
  end
   :


  • Step#4: Custom index view to paginate
#  vim app/views/products/index.html.erb
   :
</table>
<%= will_paginate @products %>
<br>
   :


  • Step#5: Custom CSS stylesheets
# vim app/assets/stylesheets/products.scss
.pagination {
  .previous_page { background:#7777ff; color:white }
  .previous_page.disabled { background: white; color: #777 }
  .current { background: yellow }
  .next_page { background: #7777ff; color: white }
  .next_page.disabled { background: white; color: #777 }
}


  • Step#6: Making will_paginate prettier# 


2.2. ActiveRecord Association, Reference, Lookup Table, Foreign Key

This post shows how to create 2 models, where a model referencing another model, on index.html viewer shows attributes from referenced model.

a) HOWTO

  • Step#1: Generate 2 models
# rails generate scaffold Author name:string
# rails generate scaffold Book   title:string publishedAt:date author:references


  • Step#2: Let's check 2 models created and association
cat app/models/author.rb
class Author < ApplicationRecord
end
# cat app/models/book.rb
class Book < ApplicationRecord
  belongs_to :author
end


  • Step#3: Let's see how books are listed on http:// ... /books. As you can see, author is shown as internal object identification. It's not much pretty, isn't it?
# vim app/views/books/index.html.erb
                   :
        <td><%= book.title %></td>
        <td><%= book.publishedAt %></td>
        <td><%= book.author %></td>
        <td><%= book.author.name %></td>

        <td><%= link_to 'Show', book %></td>
                   :

Books
Title Publishedat Author
Titulo 2017-04-14 #<Author:0x007ff9681cd4f0> Show Edit Destroy
New Book

  • Step#4: Adjust app/views/books/index.html.rb to show author name insted of internal object identification

vim app/views/books/index.html.erb
                   :
        <td><%= book.title %></td>
        <td><%= book.publishedAt %></td>
        <td><%= book.author.name %></td>
        <td><%= book.author.name %></td>

        <td><%= link_to 'Show', book %></td>
                   :

Books
Title Publishedat Author
Titulo 2017-04-14 Machado de Assis Show Edit Destroy
New Book



  • Step#5: Adjust app/views/books/show.html.rb to show author name insted of internal object identification



vim app/views/books/show.html.erb
                   :
<p>
  <strong>Author:</strong>
  <%= @book.author.name %>
</p>
                   :

Books
Title: Titulo
Publishedat: 2017-04-14
Author: Machado de Assis
Edit | Back

Nenhum comentário:

Postar um comentário