2012-05-15 39 views
0

当我开始获取这些错误消息时,我几乎完成了本教程。有人能给我一个从哪里开始找出错误的起点。你们都需要看什么文件?Micheal Hartls第11章rails教程

失败:

1) MicropostsController DELETE 'destroy' for an unauthorized user should deny access 
    Failure/Error: response.should redirect_to(root_path) 
     Expected response to be a redirect to <http://test.host/> but was a redirect to <http://test.host/signin> 
    # ./spec/controllers/microposts_controller_spec.rb:72:in `block (4 levels) in <top (required)>' 

    2) UsersController GET 'new' should be successful 
    Failure/Error: get :new 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000005a62f70> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./spec/controllers/users_controller_spec.rb:131:in `block (3 levels) in <top (required)>' 

    3) UsersController GET 'new' should have the right title 
    Failure/Error: get :new 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000004242df0> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./spec/controllers/users_controller_spec.rb:136:in `block (3 levels) in <top (required)>' 

    4) UsersController GET 'new' POST 'create' failure should have the right title 
    Failure/Error: post :create, :user => @attr 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000004c96518> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/controllers/users_controller.rb:29:in `create' 
    # ./spec/controllers/users_controller_spec.rb:148:in `block (5 levels) in <top (required)>' 

    5) UsersController GET 'new' POST 'create' failure should render the 'new' page 
    Failure/Error: post :create, :user => @attr 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000056482c8> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/controllers/users_controller.rb:29:in `create' 
    # ./spec/controllers/users_controller_spec.rb:152:in `block (5 levels) in <top (required)>' 

    6) UsersController GET 'new' POST 'create' failure should not create a user 
    Failure/Error: post :create, :user => @attr 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000051f8038> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/controllers/users_controller.rb:29:in `create' 
    # ./spec/controllers/users_controller_spec.rb:157:in `block (6 levels) in <top (required)>' 
    # ./spec/controllers/users_controller_spec.rb:156:in `block (5 levels) in <top (required)>' 

    7) UsersController GET 'new' GET 'edit' should be succesful 
    Failure/Error: get :edit, :id => @user 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000053301f8> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./spec/controllers/users_controller_spec.rb:197:in `block (4 levels) in <top (required)>' 

    8) UsersController GET 'new' GET 'edit' should have the right title 
    Failure/Error: get :edit, :id => @user 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x0000000508af20> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./spec/controllers/users_controller_spec.rb:202:in `block (4 levels) in <top (required)>' 

    9) UsersController GET 'new' GET 'edit' should have a link to change the gravatar 
    Failure/Error: get :edit, :id => @user 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000059d7d08> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./spec/controllers/users_controller_spec.rb:207:in `block (4 levels) in <top (required)>' 

    10) UsersController GET 'new' PUT 'update' should change the user's attributes 
    Failure/Error: put :update, :id => @user, :user => @attr 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000004cf6990> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/controllers/users_controller.rb:43:in `update' 
    # ./spec/controllers/users_controller_spec.rb:247:in `block (4 levels) in <top (required)>' 

    11) UsersController GET 'new' PUT 'update' should have the right title 
    Failure/Error: put :update, :id => @user, :user => @attr 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000054200b8> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/controllers/users_controller.rb:43:in `update' 
    # ./spec/controllers/users_controller_spec.rb:255:in `block (4 levels) in <top (required)>' 

    12) UsersController GET 'new' PUT 'update' failure should render the 'edit' page 
    Failure/Error: put :update, :id => @user, :user => @attr 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000057c0448> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/controllers/users_controller.rb:43:in `update' 
    # ./spec/controllers/users_controller_spec.rb:231:in `block (5 levels) in <top (required)>' 

    13) FriendlyForwardings should forward to the requested page after signin 
    Failure/Error: click_button 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x0000000541df48> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080' 
    # ./spec/requests/friendly_forwardings_spec.rb:10:in `block (2 levels) in <top (required)>' 

    14) LayoutLinks should have an signup page at '/signup' 
    Failure/Error: get '/signup' 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000003980370> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./spec/requests/layout_links_spec.rb:28:in `block (2 levels) in <top (required)>' 

    15) LayoutLinks should have the right links on the layout 
    Failure/Error: click_link "Sign up now!" 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000058c3098> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./spec/requests/layout_links_spec.rb:47:in `block (2 levels) in <top (required)>' 

    16) creation failure should not make a new micropost 
    Failure/Error: fill_in :micropost_content, :with => "" 
    Webrat::NotFoundError: 
     Could not find field: :micropost_content 
    # ./spec/requests/microposts_spec.rb:17:in `block (4 levels) in <top (required)>' 
    # ./spec/requests/microposts_spec.rb:15:in `block (3 levels) in <top (required)>' 

    17) creation success should make a new micropost 
    Failure/Error: fill_in :micropost_content, :with => content 
    Webrat::NotFoundError: 
     Could not find field: :micropost_content 
    # ./spec/requests/microposts_spec.rb:30:in `block (4 levels) in <top (required)>' 
    # ./spec/requests/microposts_spec.rb:28:in `block (3 levels) in <top (required)>' 

    18) Users signup failure should not make a new user 
    Failure/Error: visit signup_path 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000005766c40> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./spec/requests/users_spec.rb:11:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/users_spec.rb:10:in `block (4 levels) in <top (required)>' 

    19) Users signup success should make a new 
    Failure/Error: visit signup_path 
    ActionView::Template::Error: 
     undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000059e8bd0> 
    # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520' 
    # ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120' 
    # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100' 
    # ./spec/requests/users_spec.rb:27:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/users_spec.rb:26:in `block (4 levels) in <top (required)>' 

Finished in 4.31 seconds 
124 examples, 19 failures 

Failed examples: 

rspec ./spec/controllers/microposts_controller_spec.rb:70 # MicropostsController DELETE 'destroy' for an unauthorized user should deny access 
rspec ./spec/controllers/users_controller_spec.rb:130 # UsersController GET 'new' should be successful 
rspec ./spec/controllers/users_controller_spec.rb:135 # UsersController GET 'new' should have the right title 
rspec ./spec/controllers/users_controller_spec.rb:147 # UsersController GET 'new' POST 'create' failure should have the right title 
rspec ./spec/controllers/users_controller_spec.rb:151 # UsersController GET 'new' POST 'create' failure should render the 'new' page 
rspec ./spec/controllers/users_controller_spec.rb:155 # UsersController GET 'new' POST 'create' failure should not create a user 
rspec ./spec/controllers/users_controller_spec.rb:196 # UsersController GET 'new' GET 'edit' should be succesful 
rspec ./spec/controllers/users_controller_spec.rb:201 # UsersController GET 'new' GET 'edit' should have the right title 
rspec ./spec/controllers/users_controller_spec.rb:206 # UsersController GET 'new' GET 'edit' should have a link to change the gravatar 
rspec ./spec/controllers/users_controller_spec.rb:246 # UsersController GET 'new' PUT 'update' should change the user's attributes 
rspec ./spec/controllers/users_controller_spec.rb:254 # UsersController GET 'new' PUT 'update' should have the right title 
rspec ./spec/controllers/users_controller_spec.rb:230 # UsersController GET 'new' PUT 'update' failure should render the 'edit' page 
rspec ./spec/requests/friendly_forwardings_spec.rb:4 # FriendlyForwardings should forward to the requested page after signin 
rspec ./spec/requests/layout_links_spec.rb:27 # LayoutLinks should have an signup page at '/signup' 
rspec ./spec/requests/layout_links_spec.rb:38 # LayoutLinks should have the right links on the layout 
rspec ./spec/requests/microposts_spec.rb:14 # creation failure should not make a new micropost 
rspec ./spec/requests/microposts_spec.rb:26 # creation success should make a new micropost 
rspec ./spec/requests/users_spec.rb:8 # Users signup failure should not make a new user 
rspec ./spec/requests/users_spec.rb:25 # Users signup success should make a new 

# Finished at 2012-05-15 16:41:20 

这是_error_messages.html.erb

<% if object.errors.any? %> 

    <div id="error_explanation"> 
     <h2> 
      <%= pluralize(object.errors.count, "error") %> 
      prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved: 
      </h2> 
      <p>There were problems with the following fields:</p> 
       <ul> 
       <% object.errors.full_messages.each do |message| %> 

       <li><%= message %></li> 
       <% end %> 
       </ul> 
      </div> 
    <% end %> 

这是我_micropost_form.html.erb

<%= form_for(@micropost) do |f| %> 


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

<div class="field"> 
    <%= f.text_area :content %> 
    </div> 
<div class="=actions"> 
    <%= f.submit "Submit" %> 

    </div> 
<%end%> 

这是_fields.html.erb

<%= render 'shared/error_messages'%> 
<div class="field"> 
    <%= f.label :name %><br/> 
    <%= f.text_field :name%> 

</div> 


<div class="field"> 
    <%= f.label :email %><br/> 
    <%= f.text_field :email %> 

</div> 


<div class="field"> 
    <%= f.label :password %><br/> 
    <%= f.password_field :password %> 

</div> 


<div class="field"> 
    <%= f.label :password_confirmation, "Confirmation" %><br/> 
    <%= f.password_field :password_confirmation %> 

</div> 

回答

1

错误2到15有一个简单的修复:您的_fields.html.erb在第一行缺少一些代码。整行内容应为:

app/views/layouts/_fields.html.erb 
<%= render 'shared/error_messages', object: f.object %> #object: f.object allows the form variable to 
       #access the associated object. 

不幸的是,由于您的相关代码未附加,我无法对其余的错误进行任何操作。

0

从本文开始,这仍然是本书的最新版本中的一个问题。

在本书前面,用户相关表单的字段被重构并放入_fields.html.erb中,shared/error_messages被渲染。

users/newusers/edit文件不需要进行修改,因为在书中说,而是需要进行修改的部分是_fields.html.erb通过object: f.objectshared/error_messages

0

我有这个问题,以及,修复它我没有调整迈克尔的例子中的代码,我只是将_fields.html.erb部分从views/users文件夹移动到views/shared文件夹。一旦进入共享文件夹,它似乎接受来自两个控制器的对象。希望这可以帮助。

0

我认为你需要去被调用_error_messages部分文件,只需确保你使用称其为以下几点:

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

这是因为你要添加的隐藏字段标签。

相关问题