-1

Jbuilder

Trước đây, việc tạo nguồn cấp dữ liệu JSON thích hợp có thể rất phức tạp. May mắn thay Ruby on Rails làm cho giao dịch với JSON dễ dàng hơn nhiều. Hôm nay chúng ta sẽ học cách sử dụng gem JBuilder , cho phép chúng ta dễ dàng xây dựng các nguồn cấp dữ liệu phức tạp.

Trước tiên, chúng ta cần phải include các gem trong gemfile của Ruby. Phiên bản gần đây của Ruby on Rails đã được tích hợp gem này ,do đó bạn không cần phải làm bất cứ điều gì. Nếu nó có trong file gemfile của bạn thì không cần phải thêm vào , nếu không hãy thêm vào gemfile :

gem 'jbuilder', '~> 1.2'

Sau đó chạy bundle install

bundle install 

Tiếp theo , ta cần 1 model và một số dữ liệu mẫu . Trong project này ta có 2 model 1 model của product 1 model của review , giờ chúng ta sẽ tạo 2 model này theo các commend duới đây

rails g model Product name price:decimal{12,2} active:boolean
rails g model Review product:references user rating:integer body:text
rake db:migrate

Tuyệt vời, giờ chúng ta cần fake một ít data để test hãy copy phần dưới đây vào file db/seed.rb

Product.delete_all
Review.delete_all

Product.create!([
  {id: 1, name: "Nintendo Wii U Premium", price: 250, active: true},
  {id: 2, name: "XBox 360 250GB", price: 250, active: true},
  {id: 3, name: "Playstation 3 500 GB", price: 239.95, active: true},
  {id: 4, name: "Nintendo Wii", price: 99.95, active: true},
  {id: 5, name: "Nintendo 3DS", price: 174.95, active: true}
])

Review.create!([
  {id: 1, product_id: 1, user: "Bob", rating: 3, body: "dated graphics.  Overpriced.  However, the games are awesome."},
  {id: 2, product_id: 1, user: "Rich", rating: 4, body: "MARIO!  'nuff Said"},
  {id: 3, product_id: 2, user: "James", rating: 5, body: "Excellent value for the money."},
  {id: 4, product_id: 2, user: "Alison", rating: 5, body: "Love it!"},
  {id: 5, product_id: 3, user: "James", rating: 4, body: "Bigger hard drive then my XBox 360.  Weak user interface though."},
  {id: 6, product_id: 4, user: "Kay", rating: 1, body: "Extremely dated.  Don't buy.  Will be discontinued soon."},
  {id: 7, product_id: 5, user: "Jed", rating: 4, body: "Awesome handheld system, but a bit overpriced."}
])

Tiếp theo chúng ta hãy chạy commend dưới để create data fake

rake db:seed

Oke, giờ chúng ta đã có data fake của product và reviews , tiếp theo chúng ta sẽ tạo các mối quan hệ cho chúng. Đầu tiên mở file models/product.rb và thêm vào dòng sau

class Product < ActiveRecord::Base
  has_many :reviews
end

Tốt giờ hãy thêm vào models/reviews.rb như sau:

class Review < ActiveRecord::Base
  belongs_to :product
end

Với model , quan hệ và database fake đã tạo chúng ta chaỵ commands sau để create controller

rails g controller products index

Giờ cần thay đổi một chút trong file config/routes.rb

# get "products/index"
resources :products, only: [:index]

Oke, hãy tạo thêm file app/views/products với nội dung sau

json.products do
end

Hãy truy cập vào link http://localhost:3000/products.json để check kết quả, nhưng vẫn chưa xong chúng ta cần làm thêm một vài bước nữa .Mở app/controllers/products_controller.rb và thêm vào

class ProductsController < ApplicationController
  def index
    @products = Product.all
  end
end

Tiếp đó là file app/views/products/index.json.jbulder

json.products @products do |product|
  json.name product.name
  json.price number_to_currency product.price
  json.active product.active
end

Oke tuyệt vời hãy refesh trang và bạn sẽ thâý list sản phẩm format của json. Nhưng hãy sửa lại 1 chút để code tốt hơn

json.products @products do |product|
  json.name product.name
  json.price number_to_currency product.price
  json.active product.active

  json.reviews product.reviews do |review|
    json.user review.user
    json.rating review.rating
    json.body review.body
  end
end

Done , giờ hãy refesh trang lại và thưởng thức tất cả đã oke. Thank for reading!


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí