1

私は Ruby on Rails チュートリアル 3 を実行していて、うまくやっていますが、解決されていない問題に出くわしました。仕様を実行すると、2 つのテストが失敗します。

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

本のコードと比較できる限り自分のコードを検索しましたが、何も思いつきませんでした。私が見逃したのは愚かな小さなことだと確信しています.2番目のペア(またはそれ以上;)の目を大いに感謝します.

ここに私のテストがあります:

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

私がどこを見るべきかについての考えは大歓迎です。

4

1 に答える 1

1

問題が仕様ファイルに現れていても、問題の原因は仕様ファイルにありません。講師の Michael Hartl は、次の記述を変更します。

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

声明。つまり、最初のステートメントに「, :object => f.object」を追加します。そして、元のステートメントを含むすべてのファイルを調べて、それらを 2 番目のステートメントに変更する必要があります。それらのいずれかを見逃すと、これらのエラーが発生します。具体的には、次のファイル (および元のステートメントが含まれている可能性のあるその他のファイル) を調べます。

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
于 2011-10-14T10:28:12.417 に答える