#2 楼 @Ryan 恩,十分感谢你们的帮助!谢谢!这段时间在准备考试,没鼓捣!问题就搁置了!万分抱歉! #3 楼 @reyesyang 恩,十分感谢你们的帮助!谢谢!这段时间在准备考试,没鼓捣!问题就搁置了!万分抱歉!
我的模型是这样的:
用户表:Users has_one : cart
购物车表:Carts belongs_to :user (表中通过 user_id 关联)
用户表与购物车表为一对一的关系。
之后对于购物车与商品之间我才用多对多,通过关系表连接
购物车表:has_many :line_items
商品表:has_many :line_items
关系表:belongs_to :flower
belongs_to :cart
| id | user_id | created_at | updated_at |
+----+---------+---------------------+---------------------+
| 4 | NULL | 2013-10-09 09:19:30 | 2013-10-09 09:19:30 |
似乎这个方法需要修改,我一直没能该对
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
因为当购物车 cart 为空时,就会报错。可能是由于 cart 模型验证,user_id 为空,cart 没能建立。但奇怪的是 line_items 表中数据会写入,此时 cart_id 为空。
控制器代码
cart_controller.rb
def create
@cart = Cart.new(cart_params)
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render action: 'show', status: :created, location: @cart }
else
format.html { render action: 'new' }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
def cart_params
params.require(:cart).permit(:user_id)
# :cart.user_id = current_user.id
end line_items_controller.rb def create @cart = current_cart # @cart = Cart.new() flower = Flower.find(params[:flower_id]) @line_item = @cart.add_flower(flower.id) # @line_item = @cart.line_items.build(:flower => flower)
if @line_item.save redirect_to @line_item.cart # redirect_to flowers_url else render 'new' end end
不知道你这个问题解决没有,我也遇到类似问题。mlb0903 @Teddy
#10 楼 @lmorenbit 还是非常感谢你的帮助!
#6 楼 @lmorenbit 服务器就不定的处理这个问题,重复的!
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE (`users`.`email` = '[email protected]' AND `users`.`id` != 8) LIMIT 1
SQL (0.3ms) UPDATE `users` SET `photo_updated_at` = '2013-10-07 02:53:52', `updated_at` = '2013-10-07 02:53:52', `photo_file_name` = 'nightmash.jpg' WHERE `users`.`id` = 8
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: convert '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]' -crop 295x295+200+115 -auto-orient -resize "300x300>" '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-faee8k'
Command :: file -b --mime '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-faee8k'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: convert '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]' -crop 295x295+200+115 -auto-orient -resize "100x100>" '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-td1rv4'
Command :: file -b --mime '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-td1rv4'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: convert '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]' -crop 295x295+200+115 -auto-orient -resize "500x500>" '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-9vm6w8'
Command :: file -b --mime '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-9vm6w8'
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/original/nightmash.jpg
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/medium/nightmash.jpg
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/thumb/nightmash.jpg
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/large/nightmash.jpg
User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE (`users`.`email` = '[email protected]' AND `users`.`id` != 8) LIMIT 1
SQL (0.3ms) UPDATE `users` SET `photo_updated_at` = '2013-10-07 02:53:53', `updated_at` = '2013-10-07 02:53:52', `photo_file_name` = 'nightmash.jpg' WHERE `users`.`id` = 8
#6 楼 @lmorenbit 刚学两个多月,我再鼓捣鼓捣!!
#6 楼 @lmorenbit 非常感谢,问题解决了部分!现在是裁剪实现了,但是服务器会崩溃.....如果还是将这四个参数传过去的话,重启之后还是崩!只有删掉,会正常。而且图片裁剪爱成功了!