From 094f23609e5d03b5db20b342adacb464e6f2b517 Mon Sep 17 00:00:00 2001 From: Stewart Farley Date: Mon, 13 Jul 2015 10:50:09 -0700 Subject: [PATCH 1/3] hw 5 part 1 --- app/controllers/products_controller.rb | 20 ++++++++++++++++++++ app/views/products/create.html.erb | 21 ++------------------- app/views/products/index.html.erb | 9 ++++----- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index f1ad12d..e8a2993 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,2 +1,22 @@ class ProductsController < ApplicationController + def index + @products = Product.includes(:user).all + respond_to do |format| + format.html + format.json {render :json => @products} + end + end + def create + @product = Product.new(params[:product]) + + respond_to do |format| + if @product.save + format.html {render :action => 'create'} + format.json {render :json => @product} + else + format.html {render :action => 'new'} + format.json {render :json => @product.errors, :status => :unprocessable_entity} + end + end + end end diff --git a/app/views/products/create.html.erb b/app/views/products/create.html.erb index b5028be..fafd15f 100644 --- a/app/views/products/create.html.erb +++ b/app/views/products/create.html.erb @@ -1,21 +1,4 @@

Create View

-<%= params[:product].inspect %> - - - -<% product = Product.new(params[:product]) %> -<%= product.save %> -<%= product.inspect %> - -
-<% product.errors.inspect %> - -<% if product.save %> -

Congrats You Created a New Product

- Your product looks like <%= product.inspect %> -<% else %> -

Your product was not saved!!

- <%= product.errors.full_messages %> - Please go back in your browser and fix the problem -<% end %> \ No newline at end of file +

Product Created Successfully

+ <% @product.name %> added to the website, it costs: $<%= @product.price %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index a652b03..2a9fd1c 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -1,11 +1,10 @@

Products in the market

-<% lots_of_products = Product.includes(:user).all %> \ No newline at end of file + From 1170839e9c35fb8efa390f926db4b4f9571e2659 Mon Sep 17 00:00:00 2001 From: Stewart Farley Date: Tue, 14 Jul 2015 12:22:09 -0700 Subject: [PATCH 2/3] hw 5pt 2 complete --- app/controllers/products_controller.rb | 3 +++ app/views/products/new.html.erb | 32 ++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e8a2993..c6e4cb4 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,4 +1,7 @@ class ProductsController < ApplicationController + def new + @product = Product.new + end def index @products = Product.includes(:user).all respond_to do |format| diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb index 67974d9..d50911a 100644 --- a/app/views/products/new.html.erb +++ b/app/views/products/new.html.erb @@ -1,14 +1,28 @@

Let's add new products!

-
-
- +<% if @product.errors.any? %> +
+

<%= pluralize(@product.errors.count, "error") %> prohibited this user from being saved:

-
-
- +
    + <% @product.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+<% end %> -
- -
+<%= form_for(@product) do |f| %> +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :price %>
+ <%= f.text_field :price %> +
+
+ <%= f.submit %> +
+ <% end %> From 01913ef409535d1694393221b0008375403ea708 Mon Sep 17 00:00:00 2001 From: Stewart Farley Date: Wed, 15 Jul 2015 10:00:36 -0700 Subject: [PATCH 3/3] week 5 hw final --- app/controllers/products_controller.rb | 26 +++++++++++++++++++++++++ app/views/layouts/application.html.erb | 6 +++++- app/views/products/_form.html.erb | 27 ++++++++++++++++++++++++++ app/views/products/edit.html.erb | 5 +++++ app/views/products/new.html.erb | 27 +------------------------- app/views/products/show.html.erb | 12 ++++++++++++ config/routes.rb | 5 +---- 7 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 app/views/products/_form.html.erb create mode 100644 app/views/products/edit.html.erb create mode 100644 app/views/products/show.html.erb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index c6e4cb4..7fc210d 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,4 +1,30 @@ class ProductsController < ApplicationController + def destroy + @product = Product.where(:id => params[:id]).first + @product.destroy + respond_to do |format| + format.html { redirect_to products_url } + format.json { head :no_content } + end + end + def update + @product = Product.where(:id => params[:id]).first + respond_to do |format| + if @product.update_attributes(params[:product]) + format.html { redirect_to @product, :notice => 'Product was succesfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit"} + format.json { render json: @product.errors, :status => :unprocessable_entity } + end + end + end + def edit + @product = Product.where(:id => params[:id]).first + end + def show + @product = Product.where(:id => params[:id]).first + end def new @product = Product.new end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0f7a4c8..54048aa 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,9 +8,13 @@ -<%= link_to "a list of users", users_path %> | +<%= link_to "a list of users", users_path %> | <%= link_to "a list of products", products_path %> +<% if flash[:notice].present? %> +

<%= flash[:notice] %>

+<% end %> + <%= yield %> diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb new file mode 100644 index 0000000..6af225d --- /dev/null +++ b/app/views/products/_form.html.erb @@ -0,0 +1,27 @@ + +<%= form_for(@product) do |f| %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :price %>
+ <%= f.text_field :price %> +
+
+ <%= f.submit %> +
+ <% end %> + + <% if @product.errors.any? %> +
+

<%= pluralize(@product.errors.count, "error") %> prohibited this user from being saved:

+ +
    + <% @product.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+<% end %> diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 0000000..b7c3f64 --- /dev/null +++ b/app/views/products/edit.html.erb @@ -0,0 +1,5 @@ +

Editing Product

+ +<%= render 'form' %> + +<% link_to 'Back', products_path %> diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb index d50911a..bb9f906 100644 --- a/app/views/products/new.html.erb +++ b/app/views/products/new.html.erb @@ -1,28 +1,3 @@

Let's add new products!

-<% if @product.errors.any? %> -
-

<%= pluralize(@product.errors.count, "error") %> prohibited this user from being saved:

- -
    - <% @product.errors.full_messages.each do |msg| %> -
  • <%= msg %>
  • - <% end %> -
-
-<% end %> - -<%= form_for(@product) do |f| %> - -
- <%= f.label :name %>
- <%= f.text_field :name %> -
-
- <%= f.label :price %>
- <%= f.text_field :price %> -
-
- <%= f.submit %> -
- <% end %> +<%= render :partial => 'products/form'%> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000..996561c --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,12 @@ + +

+ Name: + <%= @product.name %> +

+ +

+ Price: + <%= @product.price %> +

+ +<%= link_to 'Destroy', @product, :method => :delete, :data => { :confirm => 'Are you sure?' } %> diff --git a/config/routes.rb b/config/routes.rb index 2ea9dda..df92293 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,7 @@ ControllerExercise::Application.routes.draw do - get '/products' => 'products#index' - get '/products/new' => 'products#new' - post '/products' => 'products#create' - +resources :products resources :users # The priority is based upon order of creation: