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>'
def initialize
within your model? – Uxoriousinitialize
method. I searched everywhere if I didn't overridenew
orinitialize
method but I didn't find anything. – Overtrumpreturn (@reviews.last.hr_validate || @reviews.last.manager_validate)
in your check_last_validate method? and you are not initialising your review properly, you don't have anything in parameters – Margiemarginhas_many :reviews, order: 'id ASC', dependent: :destroy
inUser
model) to verify if the last review is validated or not. But I already try to get off all conditions and it still doesn't work. Even when I try with a simple line inrails console
. – Overtrump@review = Review.new
,@review.save
and theredirect_to
indef create
to isolate the problem, and it still doesn't work. – Overtrumpsend
, as this could conflict with the ruby dispatch mechanism. – Sullyprudhommesend
attributes in my filestructure.sql
and in theattr_accessible
. I thought I used a reserved word, but apparently not. I also try to renameReview
model, and same problem. So it can come from one of its attributes. – Overtrump