| @@ -0,0 +1,3 @@ | |||||
| # Place all the behaviors and hooks related to the matching controller here. | |||||
| # All this logic will automatically be available in application.js. | |||||
| # You can use CoffeeScript in this file: http://coffeescript.org/ | |||||
| @@ -0,0 +1,3 @@ | |||||
| // Place all the styles related to the Products controller here. | |||||
| // They will automatically be included in application.css. | |||||
| // You can use Sass (SCSS) here: http://sass-lang.com/ | |||||
| @@ -0,0 +1,89 @@ | |||||
| body { | |||||
| background-color: #fff; | |||||
| color: #333; | |||||
| font-family: verdana, arial, helvetica, sans-serif; | |||||
| font-size: 13px; | |||||
| line-height: 18px; | |||||
| margin: 33px; | |||||
| } | |||||
| p, ol, ul, td { | |||||
| font-family: verdana, arial, helvetica, sans-serif; | |||||
| font-size: 13px; | |||||
| line-height: 18px; | |||||
| margin: 33px; | |||||
| } | |||||
| pre { | |||||
| background-color: #eee; | |||||
| padding: 10px; | |||||
| font-size: 11px; | |||||
| } | |||||
| a { | |||||
| color: #000; | |||||
| &:visited { | |||||
| color: #666; | |||||
| } | |||||
| &:hover { | |||||
| color: #fff; | |||||
| background-color: #000; | |||||
| } | |||||
| } | |||||
| th { | |||||
| padding-bottom: 5px; | |||||
| } | |||||
| td { | |||||
| padding-bottom: 7px; | |||||
| padding-left: 5px; | |||||
| padding-right: 5px; | |||||
| } | |||||
| div { | |||||
| &.field, &.actions { | |||||
| margin-bottom: 10px; | |||||
| } | |||||
| } | |||||
| #notice { | |||||
| color: green; | |||||
| } | |||||
| .field_with_errors { | |||||
| padding: 2px; | |||||
| background-color: red; | |||||
| display: table; | |||||
| } | |||||
| #error_explanation { | |||||
| width: 450px; | |||||
| border: 2px solid red; | |||||
| padding: 7px; | |||||
| padding-bottom: 0; | |||||
| margin-bottom: 20px; | |||||
| background-color: #f0f0f0; | |||||
| h2 { | |||||
| text-align: left; | |||||
| font-weight: bold; | |||||
| padding: 5px 5px 5px 15px; | |||||
| font-size: 12px; | |||||
| margin: -7px; | |||||
| margin-bottom: 0; | |||||
| background-color: #c00; | |||||
| color: #fff; | |||||
| } | |||||
| ul li { | |||||
| font-size: 12px; | |||||
| list-style: square; | |||||
| } | |||||
| } | |||||
| label { | |||||
| display: block; | |||||
| } | |||||
| @@ -0,0 +1,74 @@ | |||||
| class ProductsController < ApplicationController | |||||
| before_action :set_product, only: [:show, :edit, :update, :destroy] | |||||
| # GET /products | |||||
| # GET /products.json | |||||
| def index | |||||
| @products = Product.all | |||||
| end | |||||
| # GET /products/1 | |||||
| # GET /products/1.json | |||||
| def show | |||||
| end | |||||
| # GET /products/new | |||||
| def new | |||||
| @product = Product.new | |||||
| end | |||||
| # GET /products/1/edit | |||||
| def edit | |||||
| end | |||||
| # POST /products | |||||
| # POST /products.json | |||||
| def create | |||||
| @product = Product.new(product_params) | |||||
| respond_to do |format| | |||||
| if @product.save | |||||
| format.html { redirect_to @product, notice: 'Product was successfully created.' } | |||||
| format.json { render :show, status: :created, location: @product } | |||||
| else | |||||
| format.html { render :new } | |||||
| format.json { render json: @product.errors, status: :unprocessable_entity } | |||||
| end | |||||
| end | |||||
| end | |||||
| # PATCH/PUT /products/1 | |||||
| # PATCH/PUT /products/1.json | |||||
| def update | |||||
| respond_to do |format| | |||||
| if @product.update(product_params) | |||||
| format.html { redirect_to @product, notice: 'Product was successfully updated.' } | |||||
| format.json { render :show, status: :ok, location: @product } | |||||
| else | |||||
| format.html { render :edit } | |||||
| format.json { render json: @product.errors, status: :unprocessable_entity } | |||||
| end | |||||
| end | |||||
| end | |||||
| # DELETE /products/1 | |||||
| # DELETE /products/1.json | |||||
| def destroy | |||||
| @product.destroy | |||||
| respond_to do |format| | |||||
| format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' } | |||||
| format.json { head :no_content } | |||||
| end | |||||
| end | |||||
| private | |||||
| # Use callbacks to share common setup or constraints between actions. | |||||
| def set_product | |||||
| @product = Product.find(params[:id]) | |||||
| end | |||||
| # Never trust parameters from the scary internet, only allow the white list through. | |||||
| def product_params | |||||
| params.require(:product).permit(:title, :description, :image_url, :price) | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,2 @@ | |||||
| module ProductsHelper | |||||
| end | |||||
| @@ -0,0 +1,2 @@ | |||||
| class Product < ApplicationRecord | |||||
| end | |||||
| @@ -0,0 +1,37 @@ | |||||
| <%= form_for(product) do |f| %> | |||||
| <% if product.errors.any? %> | |||||
| <div id="error_explanation"> | |||||
| <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2> | |||||
| <ul> | |||||
| <% product.errors.full_messages.each do |message| %> | |||||
| <li><%= message %></li> | |||||
| <% end %> | |||||
| </ul> | |||||
| </div> | |||||
| <% end %> | |||||
| <div class="field"> | |||||
| <%= f.label :title %> | |||||
| <%= f.text_field :title %> | |||||
| </div> | |||||
| <div class="field"> | |||||
| <%= f.label :description %> | |||||
| <%= f.text_area :description, rows: 10, cols: 60 %> | |||||
| </div> | |||||
| <div class="field"> | |||||
| <%= f.label :image_url %> | |||||
| <%= f.text_field :image_url %> | |||||
| </div> | |||||
| <div class="field"> | |||||
| <%= f.label :price %> | |||||
| <%= f.text_field :price %> | |||||
| </div> | |||||
| <div class="actions"> | |||||
| <%= f.submit %> | |||||
| </div> | |||||
| <% end %> | |||||
| @@ -0,0 +1,2 @@ | |||||
| json.extract! product, :id, :title, :description, :image_url, :price, :created_at, :updated_at | |||||
| json.url product_url(product, format: :json) | |||||
| @@ -0,0 +1,6 @@ | |||||
| <h1>Editing Product</h1> | |||||
| <%= render 'form', product: @product %> | |||||
| <%= link_to 'Show', @product %> | | |||||
| <%= link_to 'Back', products_path %> | |||||
| @@ -0,0 +1,33 @@ | |||||
| <p id="notice"><%= notice %></p> | |||||
| <h1>Products</h1> | |||||
| <table> | |||||
| <thead> | |||||
| <tr> | |||||
| <th>Title</th> | |||||
| <th>Description</th> | |||||
| <th>Image url</th> | |||||
| <th>Price</th> | |||||
| <th colspan="3"></th> | |||||
| </tr> | |||||
| </thead> | |||||
| <tbody> | |||||
| <% @products.each do |product| %> | |||||
| <tr> | |||||
| <td><%= product.title %></td> | |||||
| <td><%= product.description %></td> | |||||
| <td><%= product.image_url %></td> | |||||
| <td><%= product.price %></td> | |||||
| <td><%= link_to 'Show', product %></td> | |||||
| <td><%= link_to 'Edit', edit_product_path(product) %></td> | |||||
| <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td> | |||||
| </tr> | |||||
| <% end %> | |||||
| </tbody> | |||||
| </table> | |||||
| <br> | |||||
| <%= link_to 'New Product', new_product_path %> | |||||
| @@ -0,0 +1 @@ | |||||
| json.array! @products, partial: 'products/product', as: :product | |||||
| @@ -0,0 +1,5 @@ | |||||
| <h1>New Product</h1> | |||||
| <%= render 'form', product: @product %> | |||||
| <%= link_to 'Back', products_path %> | |||||
| @@ -0,0 +1,24 @@ | |||||
| <p id="notice"><%= notice %></p> | |||||
| <p> | |||||
| <strong>Title:</strong> | |||||
| <%= @product.title %> | |||||
| </p> | |||||
| <p> | |||||
| <strong>Description:</strong> | |||||
| <%= @product.description %> | |||||
| </p> | |||||
| <p> | |||||
| <strong>Image url:</strong> | |||||
| <%= @product.image_url %> | |||||
| </p> | |||||
| <p> | |||||
| <strong>Price:</strong> | |||||
| <%= @product.price %> | |||||
| </p> | |||||
| <%= link_to 'Edit', edit_product_path(@product) %> | | |||||
| <%= link_to 'Back', products_path %> | |||||
| @@ -0,0 +1 @@ | |||||
| json.partial! "products/product", product: @product | |||||
| @@ -1,3 +1,4 @@ | |||||
| Rails.application.routes.draw do | Rails.application.routes.draw do | ||||
| resources :products | |||||
| # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||||
| end | end | ||||
| @@ -0,0 +1,12 @@ | |||||
| class CreateProducts < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| create_table :products do |t| | |||||
| t.string :title | |||||
| t.text :description | |||||
| t.string :image_url | |||||
| t.decimal :price, precision: 8, scale: 2 | |||||
| t.timestamps | |||||
| end | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,24 @@ | |||||
| # This file is auto-generated from the current state of the database. Instead | |||||
| # of editing this file, please use the migrations feature of Active Record to | |||||
| # incrementally modify your database, and then regenerate this schema definition. | |||||
| # | |||||
| # Note that this schema.rb definition is the authoritative source for your | |||||
| # database schema. If you need to create the application database on another | |||||
| # system, you should be using db:schema:load, not running all the migrations | |||||
| # from scratch. The latter is a flawed and unsustainable approach (the more migrations | |||||
| # you'll amass, the slower it'll run and the greater likelihood for issues). | |||||
| # | |||||
| # It's strongly recommended that you check this file into your version control system. | |||||
| ActiveRecord::Schema.define(version: 20161115133900) do | |||||
| create_table "products", force: :cascade do |t| | |||||
| t.string "title" | |||||
| t.text "description" | |||||
| t.string "image_url" | |||||
| t.decimal "price", precision: 8, scale: 2 | |||||
| t.datetime "created_at", null: false | |||||
| t.datetime "updated_at", null: false | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,48 @@ | |||||
| require 'test_helper' | |||||
| class ProductsControllerTest < ActionDispatch::IntegrationTest | |||||
| setup do | |||||
| @product = products(:one) | |||||
| end | |||||
| test "should get index" do | |||||
| get products_url | |||||
| assert_response :success | |||||
| end | |||||
| test "should get new" do | |||||
| get new_product_url | |||||
| assert_response :success | |||||
| end | |||||
| test "should create product" do | |||||
| assert_difference('Product.count') do | |||||
| post products_url, params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } } | |||||
| end | |||||
| assert_redirected_to product_url(Product.last) | |||||
| end | |||||
| test "should show product" do | |||||
| get product_url(@product) | |||||
| assert_response :success | |||||
| end | |||||
| test "should get edit" do | |||||
| get edit_product_url(@product) | |||||
| assert_response :success | |||||
| end | |||||
| test "should update product" do | |||||
| patch product_url(@product), params: { product: { description: @product.description, image_url: @product.image_url, price: @product.price, title: @product.title } } | |||||
| assert_redirected_to product_url(@product) | |||||
| end | |||||
| test "should destroy product" do | |||||
| assert_difference('Product.count', -1) do | |||||
| delete product_url(@product) | |||||
| end | |||||
| assert_redirected_to products_url | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,13 @@ | |||||
| # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | |||||
| one: | |||||
| title: MyString | |||||
| description: MyText | |||||
| image_url: MyString | |||||
| price: 9.99 | |||||
| two: | |||||
| title: MyString | |||||
| description: MyText | |||||
| image_url: MyString | |||||
| price: 9.99 | |||||
| @@ -0,0 +1,7 @@ | |||||
| require 'test_helper' | |||||
| class ProductTest < ActiveSupport::TestCase | |||||
| # test "the truth" do | |||||
| # assert true | |||||
| # end | |||||
| end | |||||