Tuesday, May 17, 2016

Railscast Episode 190 Upgrade Attempt to Rails 5

rails g model category name
rails g model product name price:decimal category:references description:text custom_url

rails g controller categories
rails g controller products

class Category < ApplicationRecord
  has_many :products
end

Rails.application.routes.draw do
  resources :products
  resources :categories
 
  root 'products#index'
end

Category.delete_all
['Electronics', 'Office Supplies', 'Toys', 'Clothing', 'Groceries'].each do |name|
  Category.create!(name: name)
end

Product.delete_all
categories = Category.all
words = File.readlines("/usr/share/dict/words")
lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
25.times do
  Product.create!(name: words.pop.titleize,
                  category: categories.sample,
                  description: lorem, price: [4.99, 9.99, 14.99, 19.99, 29.99].sample)
end


rails db:seed

Use SelectorGadget to find the CSS selector to use in the script.

rake fetch_prices rake task is broken because Walmart UI has changed.

No comments:

Post a Comment