ArgumentError -调用.all时参数数错误

时间:2022-06-28 00:06:06

I am very new to RoR and although I have done quite an extensive amount of searching on * and Google, I can't seem to solve this problem.

我对RoR非常陌生,虽然我在*和谷歌上做了大量的搜索,但我似乎无法解决这个问题。

In my controller, I have the following code to initialize the @coupon_categories instance variable, and continuously get this error in my index method:

在我的控制器中,我有以下代码来初始化@coupon_categories实例变量,并在我的索引方法中不断地得到这个错误:

wrong number of arguments (0 for 1)

This is my CouponCategoryController file:

这是我的CouponCategoryController文件:

class CouponCategoryController < ApplicationController

   # Implemented when user input taken
   def index
     @coupon_categories = CouponCategory.all
   end

   def new
     @coupon_category = CouponCategory.new(params[:coupon_category])
   end

coupon_category.rb:

coupon_category.rb:

class CouponCategory < ActiveRecord::Base

  has_many :coupons, :dependent => destroy # destroys coupons dependent on coupon_category  

end

Any insight would be greatly appreciated! Thank you :)

如有任何见解,将不胜感激!谢谢你:)

Edit: Here is my view file, along with the full error messages. index.html.erb:

编辑:这是我的视图文件,以及完整的错误消息。index.html.erb:

<h1> Create A Coupon Category! </h1>

<%= form_for :coupon_category do |f| %>
 Category Name: <%= f.text_field "name" %><br />
 Category expiration date (YYYY-MM-DD): <%= f.text_field "date_expired" %><br />
     <%= f.submit %>
<% end %>

<%= render :partial => 'coupon/index' %>

Full error message:

完整的错误信息:

ArgumentError in CouponCategoryController#index

wrong number of arguments (0 for 1)

app/models/coupon_category.rb:2:in `<class:CouponCategory>'
app/models/coupon_category.rb:1:in `<top (required)>'
app/controllers/coupon_category_controller.rb:5:in `index'

Thank you for your help!

谢谢你的帮助!

1 个解决方案

#1


4  

The problem may be

这个问题可能是

has_many :coupons, :dependent => destroy

The destroy should be a symbol, :destroy.

销毁应该是一个符号:销毁。

has_many :coupons, :dependent => :destroy

#1


4  

The problem may be

这个问题可能是

has_many :coupons, :dependent => destroy

The destroy should be a symbol, :destroy.

销毁应该是一个符号:销毁。

has_many :coupons, :dependent => :destroy