diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index f1ad12d..7fc210d 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,2 +1,51 @@ 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 + 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/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:

+ + +
+<% 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/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/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 + diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb index 67974d9..bb9f906 100644 --- a/app/views/products/new.html.erb +++ b/app/views/products/new.html.erb @@ -1,14 +1,3 @@

Let's add new products!

-
-
- - -
-
- - -
- -
- +<%= 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: