【发布时间】:2023-04-10 12:14:02
【问题描述】:
我正在使用 Sinatra 编写 Shop。我实现了添加到篮子,但我无法从篮子中删除。
我的班级应用:
get "/basket" do #working
products_in_basket = FetchBasket.new.call
erb :"basket/show", locals: { basket: products_in_basket }
end
post "/basket" do #working
AddToBasket.new(params).call
redirect "/"
end
delete "basket/:id" do # doesn't work
DeleteBasket.new(params).call
redirect "/"
end
我的删除篮:
module Shop
class DeleteBasket
attr_reader :product_id, :id
def initialize(params)
@id = params.fetch("id").to_i
@product_id = params.fetch("product_id").to_i
end
def call
basket = FetchBaskets(id) # finds Basket instance with given id
return unless basket
reduce_basket_quantity(basket)
def reduce_basket_quantity(basket)
if basket.quantity >= 1
basket.quantity -= 1
#warehouse = FetchWarehouseProduct.new.call(product_id)
#warehouse.quantity += quantity
else
BASKET.delete(basket)
end
end
end
end
end
在视图中删除:
<td> <form action="/basket/<%=b.id%>" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="product_id" value=<%= b.product_id %>>
<input type="hidden" name="id" value=<%= b.id %>>
<button type="submit">Delete</button>
</form>
它没有按应有的方式重定向到主页,也没有将购物篮数量更改 1。它什么也不做。
【问题讨论】:
-
你使用的是什么 ORM?
-
类应该是名词,方法应该是处理所述名词的动词。有一个名为
DeleteBasket的类是没有意义的。另外,究竟是什么不起作用?你的DeleteBasket类在语法上甚至不正确。 -
什么是语法错误?
-
call方法没有匹配的end。 -
谢谢,我在复制时犯了这个错误,我在我的应用程序中已正确。问题是我被告知以这种方式上课 - 作为服务。它们是故意的动词