【问题标题】:When assigning attributes, you must pass a hash as an argument分配属性时,必须将哈希作为参数传递
【发布时间】:2014-10-16 06:55:43
【问题描述】:

我正在关注使用 Rails 4 进行敏捷 Web 开发。第 9 章购物车创建。当我想更新购物车时,我收到以下错误通知:分配属性时,您必须将哈希作为参数传递。 CartController#update.

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]是什么样的?那不是哈希吗?你能检查参数并分享结果吗?
  • 这就是日志文件中的内容。在 2014-10-17 21:10:24 +0200 为 127.0.0.1 开始 PATCH "/carts/32" 处理由 CartsController#update 作为 HTML 参数:{"utf8"=>"✓", "authenticity_token"=>" N/VxeEOEbfYQhhEcPqMnzUPZVLxZqecS4BwJjHivqi4=", "commit"=>"Update Cart", "id"=>"32"} 购物车加载 (0.1ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 32]] (0.0ms) begin transaction (0.1ms) rollback transaction Completed 500 Internal Server Error in 2ms
  • ArgumentError(分配属性时,必须将哈希值作为参数传递。):app/controllers/carts_controller.rb:49:in block in update' app/controllers/carts_controller.rb:47:in update'

标签: ruby-on-rails


【解决方案1】:

您的参数可能是ActionController::Parameters 的一个实例

如果是这样,你需要允许你想使用的属性,像这样:

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

【讨论】:

    【解决方案2】:

    试试这个: 在您的更新方法中替换

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

    if @cart.update_attributes(cart_params)
    

    在您的 cart_params 私有方法中执行以下操作:

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

    在 Rails 4 中,引入了强参数的概念,它基本上禁止在控制器中大量分配属性。这意味着曾经在模型 (attr_accessible) 中的质量分配保护现在已移至控制器。因此,在您的模型中,您不再需要使用这个:

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

    相反,您现在可以通过以下方式在控制器中执行此操作:

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

    这意味着只有attribute1,attribute2。购物车的属性 3 是可访问的,而其他是受保护的属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多