Travis Northcutt
Travis Northcutt

Reputation: 25036

What exactly is this Rails error (wrong number of arguments) telling me?

When running some test with RSpec, I get this error (multiple times):

1) User Pages edit with valid information 
     Failure/Error: visit edit_user_path(user)
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/views/users/edit.html.haml:15:in `block in _app_views_users_edit_html_haml__2466546393595631499_70225077026800'
     # ./app/views/users/edit.html.haml:6:in `_app_views_users_edit_html_haml__2466546393595631499_70225077026800'
     # ./spec/requests/user_pages_spec.rb:54:in `block (3 levels) in <top (required)>'

I'm not familiar with reading Rails error messages; is this telling me the method being called with the wrong number of arguments is in edit.html.haml, or in user_pages_spec.rb? Is it saying that edit_user_path is being called with 0 arguments?

Edit to add my code:

edit.html.haml:

- provide(:title, "Edit user")
%h1 Update your profile

.row
    .span6.offset3
        = form_for(@user) do |f|

            = f.label :name
            = f.text_field :name

            = f.label :email
            = f.text_field :email

            = f.label :password
            = f.password_field

            = f.label :password_confirmation, "Confirm Password"
            = f.password_field :password_confirmation

            = f.submit "Save changes", class: "btn btn-large btn-primary"

Relevant portion of user_pages_spec.rb:

describe "edit" do
        let(:user) { FactoryGirl.create(:user) }
        before do
            sign_in user
            visit edit_user_path(user)
        end

        describe "page" do
            it { should have_selector('h1',    text: "Update your profile") }
            it { should have_selector('title', text: "Edit user") }
        end

        describe "with invalid information" do
            before { click_button "Save changes" }

            it { should have_content('error') }
        end

        describe "with valid information" do
            let(:new_name) { "New Name" }
            let(:new_email) { "[email protected]" }
            before do
                fill_in "Name",             with: new_name
                fill_in "Email",            with: new_email
                fill_in "Password",         with: user.password
                fill_in "Confirm Password", with: user.password
                click_button "Save changes"
            end

            it { should have_selector('title', text: new_name) }
            it { should have_success_message('') }
            it { should have_link('Sign out', href: signout_path) }
            specify { user.reload.name.should == new_name }
            specify { user.reload.email.should == new_email }
        end 
    end

Upvotes: 1

Views: 6128

Answers (2)

Steph Rose
Steph Rose

Reputation: 2136

Basically, wrong number of arguments means that for the method, it thinks that you should be passing a certain number of variables in, and it's receiving the wrong amount.

In this case, it expects 1 variable to be passed in and it's receiving none.

From the looks of it, you should be looking at line 15 of your edit.html.haml file, which I'm guessing is here:

= f.label :password
= f.password_field

It looks like you're missing :password -- passing the actual variable in.

So change it to:

= f.password_field :password

and you should be good.

Upvotes: 9

agmcleod
agmcleod

Reputation: 13621

So the top of the stack trace is the last method called, and it progresses down the chain from there. This is indicating the error is in the edit file. If you pasted the whole edit file, it looks like like 15 is missing an argument to the password field. Should be:

= f.password_field :password

Upvotes: 2

Related Questions