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

I follow Agile Web Development with Rails 4. Chapter Cart 9 Creating a Recycle Bin. When I want to update the cart, I get the following error message: when assigning attributes, you must pass the hash as arguments. 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 
+6
source share
2 answers

Your parameters are probably an instance of ActionController :: Parameters

If so, you need to enable the attributes you want to use, for example:

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

Try the following: In the update method, replace

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

 if @cart.update_attributes(cart_params) 

In your private cart_params method, do the following:

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

With Rails 4, the concept of strong parameters was introduced, which basically prohibits the mass assignment of attributes in the controller. This means that the mass protection that was once in the model (attr_accessible) is now moved to the controller. Therefore, in your models you need no more :

 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 do this in your controller via:

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

This means that only attribute1, attribute2. basket attribute 3 is available, while others are protected attributes

+3
source

Source: https://habr.com/ru/post/976818/


All Articles