Commit 2346980
Changed files (2)
app
controllers
spec
controllers
app/controllers/items_controller.rb
@@ -25,10 +25,12 @@ class ItemsController < ApplicationController
def update
item = current_user.items.find(params[:id])
item.update!(secure_params)
- render nothing: true
+ redirect_to dashboard_path
end
def destroy
+ current_user.items.destroy(params[:id])
+ redirect_to dashboard_path
end
private
spec/controllers/items_controller_spec.rb
@@ -93,7 +93,7 @@ RSpec.describe ItemsController, type: :controller do
let(:my_item) { create(:item, user: user) }
let(:item_params) { build(:item).attributes }
- it 'updates the item' do
+ it "updates the item" do
patch :update, id: my_item.id, item: item_params
my_item.reload
@@ -103,6 +103,36 @@ RSpec.describe ItemsController, type: :controller do
expect(my_item.serial_number).to eql(item_params["serial_number"])
expect(my_item.purchase_price).to eql(item_params["purchase_price"])
end
+
+ it "redirects to the dashboard" do
+ patch :update, id: my_item.id, item: item_params
+
+ expect(response).to redirect_to(dashboard_path)
+ end
+ end
+
+ describe "#destroy" do
+ let(:my_item) { create(:item, user: user) }
+ let(:other_item) { create(:item, user: other_user) }
+ let(:other_user) { create(:user) }
+
+ it "deletes the item" do
+ delete :destroy, id: my_item.id
+
+ expect(Item.count).to eql(0)
+ end
+
+ it "cannot delete another persons item" do
+ delete :destroy, id: other_item.id
+
+ expect(response.status).to eql(404)
+ end
+
+ it "redirects to the dashboard" do
+ delete :destroy, id: my_item.id
+
+ expect(response).to redirect_to(dashboard_path)
+ end
end
end
end