Rails: .new的参数数错误(1表示0)



当我尝试创建一个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'

我不明白我做错了什么?我可以创建任何其他对象,但不管我怎么做,这些都会给我这个错误。我尽量不争辩,但它仍然认为有一个……

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

我的控制器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 = 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中的所有保留字,"send"没有出现在任何地方。

所以只要重命名它,现在一切都工作了。无论如何,谢谢大家的帮助。

我假设你没有任何验证在你的审查模型作为your hr_comment, manager_comment and owner_satisfaction are not initialised,如果你有像presence: true的这些字段,那么你的记录将不会保存在数据库中。在您的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

相关内容

  • 没有找到相关文章

最新更新