Rails : wrong number of arguments (1 for 0) for .new

Kilian picture Kilian · Jun 25, 2014 · Viewed 9.5k times · Source

I have this error when I try to create a Review object :

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 :

# -*- 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 [email protected]?
      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.

I tried three ways to create my object 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

Thanks.

EDIT :

When I try in my rails console this code :

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>'

Answer

Kilian picture Kilian · Jun 26, 2014

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.