Commit 0a61f524

luu <luuduong@gmail.com>
2014-12-13 16:10:39
create a new tool.
1 parent b4f69d5
Changed files (4)
app
controllers
views
admin
config
spec
app/controllers/admin/products_controller.rb
@@ -12,5 +12,10 @@ module Admin
     def show
       @product = @product_api.find(params[:id])
     end
+
+    def create
+      Tool.create(:name=>params[:name],:asin=>params[:asin])
+      redirect_to admin_products_path(params[:asin])
+    end
   end
 end
app/views/admin/products/show.html.erb
@@ -22,7 +22,7 @@
         Add to tools:
       </td>
       <td>
-        <%= form_tag do %>
+        <%= form_tag admin_products_path do %>
           <%= text_field_tag :name, @product.item_attributes['title'] %>
           <%= hidden_field_tag "asin", @product.asin %>
           <%= submit_tag "Add to tools" %>
config/routes.rb
@@ -86,7 +86,7 @@ Cake::Application.routes.draw do
     resources :blobs, only: [:index, :show]
     resources :errors, only: [:index, :create]
     resources :sessions, only: [:index, :destroy]
-    resources :products, only: [:index, :show]
+    resources :products, only: [:index, :show, :create]
     resources :charts, only: [:index]
   end
 
spec/controllers/admin/products_controller_spec.rb
@@ -0,0 +1,25 @@
+require 'rails_helper'
+module Admin
+  describe ProductsController do 
+    describe "#create" do
+      let(:admin) { create(:admin)}
+
+      before :each do
+        http_login(admin)
+      end
+
+      it "creates new tool" do
+        post :create, {:name=>"pan", :asin=>"34234"}
+
+        expect(Tool.count).to eql(1)
+        expect(Tool.first.name).to eql("pan")
+        expect(Tool.first.asin).to eql("34234")
+      end
+
+      it "redirects back to the detail page" do
+        post :create, {name: 'blah', asin: 'blah' }
+        expect(response).to redirect_to(admin_products_path('blah'))
+      end
+    end
+  end
+end