Tôi đang theo dõi Phát triển Web nhanh với Rails 4. Chương 9 Tạo giỏ hàng. Khi tôi muốn cập nhật một giỏ hàng, tôi nhận được thông báo lỗi sau: Khi gán thuộc tính, bạn phải chuyển một băm làm đối số. Cập nhật CartController #.Khi gán thuộc tính, bạn phải chuyển một giá trị băm làm đối số
class CartsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def index
@carts = Cart.all
end
def show
end
def new
@cart = Cart.new
end
def edit
end
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 :show, status: :created, location: @cart }
else
format.html { render :new }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
def update
@cart = Cart.find(params[:id])
respond_to do |format|
if @cart.update_attributes(params[:cart])
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
def destroy
@cart.destroy if @cart.id == session[:card_id]
session[:card_id] = nil
respond_to do |format|
format.html { redirect_to store_url, notice: 'Your cart is currently empty.' }
format.json { head :no_content }
end
end
private
def set_cart
@cart = Cart.find(params[:id])
end
def cart_params
params[:cart]
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
end
end
'params [: cart]' của bạn trông như thế nào? Đó không phải là một băm? Bạn có thể kiểm tra các thông số và chia sẻ kết quả. –
Đó là những gì bên trong Logfile. Đã bắt đầu PATCH "/ xe/32" cho 127.0.0.1 lúc 2014-10-17 21:10:24 +0200 Đang xử lý bởi CartsController # cập nhật dưới dạng HTML Tham số: {"utf8" => "✓", "authenticity_token" = > "N/VxeEOEbfYQhhEcPqMnzUPZVLxZqecS4BwJjHivqi4 =", "commit" => "Cập nhật giỏ hàng", "id" => "32"} Tải giỏ hàng (0,1ms) CHỌN "giỏ hàng". * TỪ "giỏ hàng" WHERE "giỏ hàng". " id "=? LIMIT 1 [["id", 32]] (0.0ms) bắt đầu giao dịch (0.1ms) giao dịch rollback Hoàn thành 500 Lỗi máy chủ nội bộ trong 2ms – Keeic
ArgumentError (Khi gán thuộc tính, bạn phải chuyển một băm làm đối số). : ứng dụng/bộ điều khiển/carts_controller.rb: 49: trong 'chặn trong bản cập nhật ' app/controllers/carts_controller.rb: 47: in' update' – Keeic