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:- https://richonrails.com/articles/getting-started-with-will-paginate
- https://hackhands.com/pagination-rails-will_paginate-gem/
- https://github.com/mislav/will_paginate/wiki
- https://gorails.com/episodes/pagination-with-will-paginate
- http://mislav.github.io/will_paginate/
a) HOWTO
- Step#1: add gem will_paginating
:
gem 'will_paginate', '~> 3.1.0'
:
- Step#2: bundle install
- Step#3: Creating MVC for Product and custom controller for paginating each 5 records for page
# 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
:
</table>
<%= will_paginate @products %>
<br>
:
- Step#5: Custom CSS stylesheets
.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 Book title:string publishedAt:date author:references
- Step#2: Let's check 2 models created and association
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?
:
<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>
:
:
<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>
:
Title: Titulo
Publishedat: 2017-04-14
Author: Machado de Assis
Edit | Back
Nenhum comentário:
Postar um comentário