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