Rails - Non-Scaffolded Read and Create - sample of training off of youtube#
The original video is here youtube: Rails - Non-Scaffolded Read and Create
create the app#
Misc things#
seeds.rb#
Product.destroy_all
1000.times do
Product.create(
title: Faker::Commerce.unique.product_name,
description: "Surprisingly this product is made out of #{Faker::Commerce.material}.",
price: Faker::Commerce.price,
stock_quantity: Faker::Number.number(3))
end
puts "Created #{Product.count} products."
Some trick re Rails forms#
<%= form_for @product, remote: false do |f| %>
> the remote: false -> during debugging of code (something relating to Ajax form submission)
# then next line - check on errors
<% if @product.errors.any? %> # this is auto-set by model
WHITELIST form fields#
def create
article = Article.new(article_params)
end
private
def article_params
params.permit(:title, :body)
end
Creating a scaffold#
rails g scaffold user name:string role:belongs_to
rails g scaffold role name:string description:string
rails g scaffold item name:string description:text 'price:decimal{5,2}', user:belongs_to