Ruby on Rails:错误数量的参数(给定0,期望1)

时间:2021-05-10 23:20:45

I have an issue with my add_item method and have trouble to understand why.

我对我的add_item方法有问题,也不明白为什么。

Here is my carts_controller.rb

这是我carts_controller.rb

class CartsController < ApplicationController
  def index
    @cart_items = CartItem.all
  end

  def add_item
    @cart_item = CartItem.new
    produit_id = params[:produit_id]
    @cart_item = CartItem.find_or_create_by(params[:produit][:produit_id])
    @cart_item.save

    binding.pry
  end
end

Here is produits/index.html.erb (where the issue comes from)

这里的/ index . html。erb(问题来自哪里)

<div id="produits-column-container">
  <% if @produits %>
    <% @produits.in_groups_of(4, false).each do |g| %>
      <% g.each do |produit| %>
      <div id="produits-row-container">
        <div id="fiche-produit-container">
            <div id="produit-img">
              <%= image_tag produit.photo %>
            </div>
            <div id="produit-nom">
              <%= produit.nom %>
            </div>
            <div id="produit-prix">
              <%= number_to_currency(produit.prix, unit: '€', format: "%n%u") %>
            </div>
            <div id="produit-au-panier">
              <%= image_tag('icon/icon-panier') %>
              <%= link_to 'Ajouter au panier', carts_add_item_path, method: :post %>
            </div>
         </div>
       </div>
      <% end %>
    <% end %>
  <% end %>
</div>

The error i'm given is :

我得到的错误是:

ArgumentError in CartsController#add_item
wrong number of arguments (given 0, expected 1)

in def add_item(produit_id)

add_item(produit_id) is related to carts_add_item_path

add_item(produit_id)与carts_add_item_path相关

I also give you the routes :

我也给你一些路线:

Rails.application.routes.draw do

  match "/mon-panier" => 'carts#index', via: :get

  post 'carts/add_item' => 'carts#add_item'

  resources :categories do
    resources :produits
  end

  resources :order_abonnements, only: [:create, :update, :delete]

  get 'livraisons_type/index'

  match "/recapitulatif" => 'recapitulatif#index', via: :get

  match "/confirmation-carte-cadeau" => 'recapitulatif#confirmation', via: :get

  match "/livraison-carte-cadeau" => 'livraison_carte#index', via: :get

  match '/activation-carte' => 'code_carte_cadeau#index', via: :get

  match "/offrir-une-box-bretonne" => 'cadeau#index', via: :get

  resources :order_items, only: [:create, :update, :destroy]

  match "/nos-box" => 'nos_box#index', via: :get

  get 'categories/index'

  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)

  match '/informations-penn-ar-box' => 'informations_penn_ar_box#index', via: :get

  match '/livraison-box-bretonne' => 'livraison_box_bretonne#index', via: :get

  match '/abonnements' => 'abonnements#index', via: :get

  devise_for :users, path: '', path_names: { sign_in: 'connexion', sign_out: 'déconnexion'}

  resources :users do
    delete 'déconnexion' => 'devise/sessions#destroy'
  end

  match '/mon-marche-breton' => 'marche_breton#index', via: :get

  root 'home#home'
end

And the logs :

日志:

Started POST "/carts/add_item" for ::1 at 2017-05-30 09:48:52 +0200
Processing by CartsController#add_item as HTML
  Parameters: {"authenticity_token"=>"QrToQUHVxjuV5cUvZYHd7tj457htfZohOkmsvNDnKv79P413xjsSfR/8RVXtdIU7/wcmhcxjkU85N13CqJkG2w=="}
  Cart Load (0.3ms)  SELECT  `carts`.* FROM `carts` WHERE `carts`.`id` = 1 LIMIT 1
Completed 500 Internal Server Error in 27ms (ActiveRecord: 14.9ms)

ArgumentError (wrong number of arguments (given 0, expected 1)):
  app/controllers/carts_controller.rb:6:in `add_item'

2 个解决方案

#1


0  

You need to pass produit_id as a parameter.

您需要将produit_id作为参数传递。

So change this line as so...

所以改变这条线。

<%= link_to 'Ajouter au panier', 
            carts_add_item_path(produit_id: produit.id), method: :post %>

And change your controller method as so...

改变你的控制器方法。

def add_item
  produit_id = params[:produit_id]
  ...

And change the find_or_create to

并将find_or_create更改为

@cart_item = CartItem.find_or_create_by(produit_id: produit_id)

This does mean that you can only have one CartItem in your entire application that points to a product... strange design.

这意味着在整个应用程序中只能有一个指向产品的CartItem……奇怪的设计。

#2


0  

There are a lot of things that are wrong with this code, but the simplest thing you could do in order to suppress the error is to change the method signature to:

这段代码有很多错误,但是为了抑制错误,您可以做的最简单的事情是将方法签名改为:

def add_item

instead of

而不是

def add_item(produit_id)

P.S. The controller is not RESTful. It's called CartsController, but in your index action you list the CartItems, not the Carts. The correct name should be CartItemsController. If you rename the controller to CartItemsController, than the index action can stay the same, but the add_item action should better be renamed to simply create. Thus, in your routes you can have:

附注:控制器不是RESTful的。它被称为CartsController,但是在你的索引操作中你列出了CartItems,而不是购物车。正确的名称应该是CartItemsController。如果将控制器重命名为CartItemsController,而不是索引操作可以保持不变,但是add_item操作应该更好地重命名为简单创建。因此,在你的路线中你可以有:

resources :cart_items, only: [:index, :create]

Also, I am not exactly sure, what is going on in the add_item method - at first you assign a new CartItem to @cart_item, but then you override this assignment with find_or_create_by.... Also, the call to save at the end of the method is redundant, as create will save the record and otherwise no modifications have been made between the find_or_create_by line and the save line.

另外,我不确定,在add_item方法——首先你分配一个新的CartItem @cart_item,但然后你覆盖这个任务find_or_create_by ....此外,在方法末尾对save的调用是多余的,因为create将保存记录,否则find_or_create_by行和save行之间没有进行修改。

#1


0  

You need to pass produit_id as a parameter.

您需要将produit_id作为参数传递。

So change this line as so...

所以改变这条线。

<%= link_to 'Ajouter au panier', 
            carts_add_item_path(produit_id: produit.id), method: :post %>

And change your controller method as so...

改变你的控制器方法。

def add_item
  produit_id = params[:produit_id]
  ...

And change the find_or_create to

并将find_or_create更改为

@cart_item = CartItem.find_or_create_by(produit_id: produit_id)

This does mean that you can only have one CartItem in your entire application that points to a product... strange design.

这意味着在整个应用程序中只能有一个指向产品的CartItem……奇怪的设计。

#2


0  

There are a lot of things that are wrong with this code, but the simplest thing you could do in order to suppress the error is to change the method signature to:

这段代码有很多错误,但是为了抑制错误,您可以做的最简单的事情是将方法签名改为:

def add_item

instead of

而不是

def add_item(produit_id)

P.S. The controller is not RESTful. It's called CartsController, but in your index action you list the CartItems, not the Carts. The correct name should be CartItemsController. If you rename the controller to CartItemsController, than the index action can stay the same, but the add_item action should better be renamed to simply create. Thus, in your routes you can have:

附注:控制器不是RESTful的。它被称为CartsController,但是在你的索引操作中你列出了CartItems,而不是购物车。正确的名称应该是CartItemsController。如果将控制器重命名为CartItemsController,而不是索引操作可以保持不变,但是add_item操作应该更好地重命名为简单创建。因此,在你的路线中你可以有:

resources :cart_items, only: [:index, :create]

Also, I am not exactly sure, what is going on in the add_item method - at first you assign a new CartItem to @cart_item, but then you override this assignment with find_or_create_by.... Also, the call to save at the end of the method is redundant, as create will save the record and otherwise no modifications have been made between the find_or_create_by line and the save line.

另外,我不确定,在add_item方法——首先你分配一个新的CartItem @cart_item,但然后你覆盖这个任务find_or_create_by ....此外,在方法末尾对save的调用是多余的,因为create将保存记录,否则find_or_create_by行和save行之间没有进行修改。