所以这是我在我的控制器中测试的动作
def add_to_cart
@cart = current_cart
if @cart.tutors.where(params[:tutor_id]).empty?
@cart.add_tutor(params[:tutor_id])
flash[:success] = 'You have successfully added the tutor to your shortlist!'
redirect_to show_path
else
flash[:danger] = 'You have already shortlisted the tutor!'
redirect_to show_path
end
end
如果购物车中存在tutor_id
,我不确定这是否是正确的测试方法。我实际上尝试过在rails控制台中测试各种检查子属性的方法。我到达Parent.Child.where(:id => X)
以检查是否有任何一个孩子有id = X。
这是调用该方法时在我的服务器(或开发日志)中显示的内容。
Started POST "/add_to_cart/11" for 116.87.14.150 at 2016-10-16 07:32:09 +0000
Cannot render console from 116.87.14.150! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by CartsController#add_to_cart as HTML
Parameters: {"authenticity_token"=>"XzHaIJMzIz9I84W5tmLuj+28SBvVP9pc1JdZOyQaYEr286JNIw4J8tKtSON7eIWsdf0Lc6ogCdT2LrdtVVxGoA==", "tutor_id"=>"11"}
cart_id = 17
Cart Load (0.2ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 17]]
(0.2ms) SELECT COUNT(*) FROM "tutors" INNER JOIN "cart_tutors" ON "tutors"."id" = "cart_tutors"."tutor_id" WHERE "cart_tutors"."cart_id" = ? AND (11) [["cart_id", 17]]
无论如何,它不断地处理else
部分,这意味着它不像我想象的那样工作,这可能意味着它没有像我打算那样检查ID。我使用正确的方法吗?如果没有,我该怎么做呢?
答案 0 :(得分:0)
你有一些错误:
@cart.tutors.where(params[:tutor_id]).empty?
它使得sql查询不正确:
WHERE "cart_tutors"."cart_id" = ? AND (11)
参见 AND(11)
尝试下一步:
@cart.tutors.where(id: params[:tutor_id]).empty?