究竟是什么Rails错误(错误的参数数量)告诉我?

时间:2012-10-16 13:07:11

标签: ruby-on-rails debugging

使用RSpec运行某些测试时,我收到此错误(多次):

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

我不熟悉阅读Rails错误消息;这告诉我使用错误数量的参数调用的方法是在edit.html.haml中,还是在user_pages_spec.rb中?是说用0参数调用edit_user_path吗?

编辑以添加我的代码:

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"

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) { "new@example.com" }
            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

2 个答案:

答案 0 :(得分:9)

基本上,错误的参数数量意味着对于该方法,它认为您应该传递一定数量的变量,并且它接收的数量错误。

在这种情况下,它希望传入1个变量并且它没有接收任何变量。

从它的外观来看,你应该看看你的edit.html.haml文件的第15行,我猜是在这里:

= f.label :password
= f.password_field

看起来你错过了:密码 - 传递实际变量。

所以改成它:

= f.password_field :password

你应该好。

答案 1 :(得分:2)

因此堆栈跟踪的顶部是最后一个被调用的方法,它从那里向下进行链接。这表示错误发生在编辑文件中。如果您粘贴了整个编辑文件,则看起来好像15缺少密码字段的参数。应该是:

= f.password_field :password