2014-10-16 14 views
6

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 
+1

'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ả. –

+0

Đó 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

+0

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

Trả lời

8

params của bạn có lẽ là một thể hiện của ActionController::Parameters

Nếu vậy, bạn cần phải cho phép các thuộc tính bạn muốn sử dụng, như vậy:

def cart_params 
    params.require(:cart).permit(:attribute1, :attribute2, :attribute3) 
end 
3

Hãy thử điều này: Trong bản cập nhật của bạn phương thức thay thế

if @cart.update_attributes(params[:cart]) 

bởi

if @cart.update_attributes(cart_params) 

Trong cart_params bạn phương pháp tư nhân thực hiện điều này:

def cart_params 
    params.require(:cart).permit(:attribute1, :attribute2, :attribute3) 
end 

Với Rails 4, khái niệm về thông số mạnh mẽ đã được giới thiệu một cách cơ bản cấm chuyển nhượng khối lượng của các thuộc tính trong bộ điều khiển. Điều này có nghĩa là việc bảo vệ khối lượng một lần trong mô hình (attr_accessible) đã được chuyển đến bộ điều khiển. Do đó trong các mô hình của bạn, bạn không nhu cầu sử dụng này:

attr_accessible :attribute1, attribute 2 #attributes that can be mass-assinged 
attr_protected :attribute3 #attribute that is protected and cannot be mass-assinged 

Thay vào đó bạn bây giờ có thể làm điều này trong điều khiển của bạn thông qua:

params.require(:cart).permit(:attribute1, :attribute2, :attribute3) 

Điều này có nghĩa rằng chỉ attribute1, attribute2. thuộc tính3 của giỏ hàng có thể truy cập được trong khi các thuộc tính khác được bảo vệ

Các vấn đề liên quan