I have this error when I try to create a Review object :
我尝试创建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'
I don't understand what I am doing wrong ? I can create any others objects, but these one give me this error, doesn't matter what I do. I try to put no argument but it still consider there is one...
我不明白我做错了什么?我可以创建任何其他对象,但这些给我这个错误,无论我做什么。我试着不提出任何争论,但它仍然认为有一个......
My model Review
:
我的模特回顾:
# -*- 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
My controller ReviewsController
:
我的控制器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
I read all same questions on SO, but nothing works. I be careful on typo, but I don't think I did a mistake.
我在SO上阅读了所有相同的问题,但没有任何作用。我在打字错误时要小心,但我不认为我犯了错误。
I tried three ways to create my object Review
:
我尝试了三种方法来创建我的对象Review:
@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 )
The last one give me the same error except :
最后一个给我同样的错误,除了:
wrong number of arguments (2 for 0)
My ruby version is 1.9.3, my rails version is 3.2.13
我的ruby版本是1.9.3,我的rails版本是3.2.13
Thanks.
谢谢。
EDIT :
When I try in my rails console this code :
当我在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
I get this error :
我收到此错误:
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>'
2 个解决方案
#1
13
Ok I find the problem... Apparently the error was caused by the attribute send
. I'm really surprise because I checked all reserved words in ruby and "send" doesn't appear anywhere.
好的,我发现问题...显然错误是由属性发送引起的。我真的很惊讶,因为我检查了红宝石中的所有保留字,“发送”没有出现在任何地方。
So just have to rename it and everything works now. Anyhow, thanks all for your help.
所以只需要重命名它,现在一切正常。无论如何,谢谢大家的帮助。
#2
0
I'm assuming you don't have any validations in your review model as your hr_comment, manager_comment and owner_satisfaction are not initialised
, if you have something like presence: true
for these fields then your record will not save in database.Try this in your rails console:
我假设您的评论模型中没有任何验证,因为您的hr_comment,manager_comment和owner_satisfaction未初始化,如果您有类似的存在:对于这些字段为true,那么您的记录将不会保存在数据库中。请在您的rails console:
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
#1
13
Ok I find the problem... Apparently the error was caused by the attribute send
. I'm really surprise because I checked all reserved words in ruby and "send" doesn't appear anywhere.
好的,我发现问题...显然错误是由属性发送引起的。我真的很惊讶,因为我检查了红宝石中的所有保留字,“发送”没有出现在任何地方。
So just have to rename it and everything works now. Anyhow, thanks all for your help.
所以只需要重命名它,现在一切正常。无论如何,谢谢大家的帮助。
#2
0
I'm assuming you don't have any validations in your review model as your hr_comment, manager_comment and owner_satisfaction are not initialised
, if you have something like presence: true
for these fields then your record will not save in database.Try this in your rails console:
我假设您的评论模型中没有任何验证,因为您的hr_comment,manager_comment和owner_satisfaction未初始化,如果您有类似的存在:对于这些字段为true,那么您的记录将不会保存在数据库中。请在您的rails console:
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