Ruby-on-rails 分配属性时,必须传递一个散列作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26398020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:57:32  来源:igfitidea点击:

When assigning attributes, you must pass a hash as an argument

ruby-on-rails

提问by Keeic

I am following Agile Web Development with Rails 4. Chapter Cart 9 Cart Creation. When I want to update a cart, I get the following Error Notification: When assigning attributes, you must pass a hash as an arguments. CartController#update.

我正在关注 Rails 4 的敏捷 Web 开发。第 9 章购物车创建。当我想更新购物车时,我收到以下错误通知:分配属性时,您必须将散列作为参数传递。购物车控制器#更新。

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

采纳答案by Robert Falkén

Your params is probably an instance of ActionController::Parameters

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

If so, you need to permit the attributes you want to use, like so:

如果是这样,您需要允许要使用的属性,如下所示:

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

回答by Aaditi Jain

Try this: In your update method replace

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

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

by

经过

if @cart.update_attributes(cart_params)

In your cart_params private method do this:

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

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

With Rails 4, the concept of strong parameters has been introduced which basically forbids mass assignment of attributes in the controller. This means that the mass assingment protection that was once in model (attr_accessible) has now been moved to the controller. Hence in your models you no moreneed to use this:

在 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

Instead you can now do this in your controller through :

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

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

This means that only attribute1, attribute2. attribute3 of cart are accessible while other are protected attributes

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