gpt4 book ai didi

ruby-on-rails - rails : wrong number of arguments (1 for 0) for . 新

转载 作者:行者123 更新时间:2023-12-02 16:31:48 24 4
gpt4 key购买 nike

当我尝试创建 Review 对象时出现此错误:

ArgumentError in ReviewsController#create

wrong number of arguments (1 for 0)

app/controllers/reviews_controller.rb:17:in `new'
app/controllers/reviews_controller.rb:17:in `create'

我不明白我做错了什么?我可以创建任何其他对象,但是这些对象给我这个错误,无论我做什么。我试图不提出任何争论,但它仍然认为有一个......

我的模型评论:

# -*- encoding : utf-8 -*-
class Review < ActiveRecord::Base

attr_accessible :global_validation_date, :hr_comment, :hr_validate, :hr_validator, :manager_comment, :manager_validate, :manager_validation_date, :manager_validator, :owner_satisfaction, :owner_validate, :owner_validation_date, :send, :user_id

belongs_to :user
has_many :review_projects, dependent: :destroy

end

我的 Controller ReviewsController:

# -*- encoding : utf-8 -*-
class ReviewsController < ApplicationController

before_filter :authenticate_user!
before_filter :check_permissions, only: :create

def index
@user = User.find(params[:user_id])
@reviews = @user.reviews
end

def create
@user = User.find(params[:user_id])
@reviews = @user.reviews
if @reviews.empty? || check_last_validate

@review = Review.new # HERE IS MY ERROR

if @review.save
render 'index', notice: "Review successfully created"
else
render 'index', notice: "An error occured on saving, please try again"
end
else
render 'index', alert: "You have to validate previous review before create a new one."
end

redirect_to user_reviews_path
end

private

def check_permissions
render_access_forbidden unless current_user == User.find(params[:user_id])
end

def check_last_validate
if !@reviews.empty?
return (@reviews.last.hr_validate || @reviews.last.manager_validate)
else
return false
end
end

end

我读了所有关于SO的相同问题,但没有任何作用。我很小心拼写错误,但我不认为我做错了。

我尝试了三种方法来创建我的对象回顾:

@review = Review.new
@review.user_id = current_user.id
@review.hr_validator = ((current_user.hr_id.nil?) ? current_user.id : current_user.hr_id )
@review.manager_validator = ((current_user.manager_id.nil?) ? current_user.id : current_user.manager_id )
@review.hr_validate = false
@review.manager_validate = false
@review.owner_validate = false
@review.hr_comment = ""
@review.manager_comment = ""
@review.owner_satisfaction = nil
@review.send = false
<小时/>
@review = Review.new(
user_id: current_user.id,
hr_validator: ((current_user.hr_id.nil?) ? current_user.id : current_user.hr_id ),
manager_validator: ((current_user.manager_id.nil?) ? current_user.id : current_user.manager_id ),
hr_validate: false,
manager_validate: false,
owner_validate: false,
hr_comment: "",
manager_comment: "",
owner_satisfaction: nil,
send: false )
<小时/>
@user.reviews.create!(
user_id: current_user.id,
hr_validator: ((current_user.hr_id.nil?) ? current_user.id : current_user.hr_id ),
manager_validator: ((current_user.manager_id.nil?) ? current_user.id : current_user.manager_id ),
hr_validate: false,
manager_validate: false,
owner_validate: false,
hr_comment: "",
manager_comment: "",
owner_satisfaction: nil,
send: false )

最后一个给了我同样的错误,除了:

wrong number of arguments (2 for 0)

我的ruby版本是1.9.3,我的rails版本是3.2.13

谢谢。

编辑:

当我在 Rails 控制台中尝试此代码时:

current_user = User.first # as you are inside rails console you wont have current_user available to you
@review = Review.new(user_id: current_user.id,
hr_validator: ((current_user.hr_id.nil?) ? current_user.id : current_user.hr_id ),
manager_validator: ((current_user.manager_id.nil?) ? current_user.id : current_user.manager_id ),
hr_validate: false,
manager_validate: false,
owner_validate: false,
hr_comment: "",
manager_comment: "",
owner_satisfaction: nil,
send: false )
@review.save

我收到此错误:

ArgumentError: wrong number of arguments (2 for 0)
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/attribute_methods/read.rb:71:in `__temp__'
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in `initialize'
from (irb):31:in `new'
from (irb):31
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/killian/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

最佳答案

好吧,我发现问题了...显然错误是由属性 send 引起的。我真的很惊讶,因为我检查了 ruby​​ 中的所有保留字,并且“发送”没有出现在任何地方。

所以只需重命名它,现在一切就可以了。不管怎样,感谢大家的帮助。

关于ruby-on-rails - rails : wrong number of arguments (1 for 0) for . 新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24411253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com