2011-06-11 96 views
1

我通過Ruby on Rails的教程3運行,並且有一個球做,但我剛剛遇到的是沒有得到解決的問題。當我運行我的規格時,兩個測試失敗。Rails的教程3,RSpec的失敗

Failures: 
    1) UsersController PUT 'update' failure should render the 'edit' page 
    Failure/Error: put :update, :id => @user, :user => @attr 
    undefined local variable or method `object' for #<#<Class:0x00000102c861c8>:0x00000101d25558> 
# ./app/views/shared/_error_messages.html.erb:3:in `_app_views_shared__error_messages_html_erb___3390867530789228804_2170854120__2806434579894406668' 
# ./app/views/users/edit.html.erb:4:in `block in _app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416' 
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416' 
# ./app/controllers/users_controller.rb:47:in `update' 
# ./spec/controllers/users_controller_spec.rb:158:in `block (4 levels) in <top (required)>' 

2) UsersController PUT 'update' failure should have the right title 
Failure/Error: put :update, :id => @user, :user => @attr 
undefined local variable or method `object' for #<#<Class:0x00000102c861c8>:0x00000101b211f8> 
# ./app/views/shared/_error_messages.html.erb:3:in `_app_views_shared__error_messages_html_erb___3390867530789228804_2170854120__2806434579894406668' 
# ./app/views/users/edit.html.erb:4:in `block in _app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416' 
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416' 
# ./app/controllers/users_controller.rb:47:in `update' 
# ./spec/controllers/users_controller_spec.rb:163:in `block (4 levels) in <top (required)>' 

我已經通過我的代碼作爲最好的,我可以比較反對它在書中的代碼搜索,我想出了什麼。我相信這是我錯過的一件愚蠢的小事,我會非常感激第二雙(或更多)眼睛。

這裏是我的測試:

describe "failure" do 
    before(:each) do 
    @attr = { :email => "", :name => "", :password => "", :password_confirmation => "" } 
    end 

    it "should render the 'edit' page" do 
    put :update, :id => @user, :user => @attr 
    response.should render_template('edit') 
    end 

    it "should have the right title" do 
    put :update, :id => @user, :user => @attr 
    response.should have_selector("title", :content => "Edit User") 
    end 
end 

這裏是從users_controller更新方法:

def update 
@user = User.find(params[:id]) 
if @user.update_attributes(params[:user]) 
    flash[:success] = "Profile updated" 
    redirect_to @user 
else 
    @title = "Edit User" 
    render 'edit' 
end 
end 

在哪裏我應該是極大的讚賞任何想法。

+1

從您的_error_messages.html.erb降臨到你的錯誤:3模板? – monocle 2011-06-11 01:48:15

+1

您可以發佈app/views/shared/_error_messages.html.erb的內容嗎? – 2011-06-11 02:02:03

+0

非常感謝你們!你指出我正確的方向。問題不在於error_messages.html.erb;它在我的edit.html.erb中,這是我應該創建它所抱怨的'object'變量的地方。你是最好的! – YuKagi 2011-06-11 03:24:02

回答

1

問題的根源並不在規範文件,即使這個問題體現在這些規範文件。教練Michael Hartl更改了以下聲明:

<%= render 'shared/error_messages' % 
        to 
<%= render 'shared/error_messages', :object => f.object %> 

聲明。換句話說,他在第一個語句中添加了「:object => f.object」。你必須查看所有具有原始聲明的文件,並將它們更改爲第二個文件。如果你錯過了任何一個,你會有這些錯誤。具體看下面的文件(及任何其他可能有原來的語句):

app/views/users/edit.html.erb 
app/views/users/fields.html.erb 
app/views/users/new.html.erb 
app/views/shared/micropost_form.html.erb 
+0

感謝您隨時關注本教程,甚至更多地評價我的更新。我很感激。 ;) – YuKagi 2011-10-17 18:12:05