迈克尔·哈特尔斯第 11 章轨道教程



当我开始收到这些错误消息时,我几乎完成了本教程。有人可以给我一个起点,从哪里开始找出问题所在。 你们都需要查看哪些文件?

失败:

  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>

错误 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.

不幸的是,我无法对您的其余错误做任何事情,因为您的相关代码没有附加。

截至本文发布时,这仍然是本书最新版本中的一个问题。

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

users/newusers/edit文件不需要修改,如书中所述,唯一需要修改的部分是_fields.html.erb传递object: f.object shared/error_messages

我也遇到了这个问题,为了解决这个问题,我没有调整迈克尔示例中的代码,我只是将_fields.html.erb部分从 views/users 文件夹移动到 views/shared 文件夹。进入共享文件夹后,它似乎接受来自两个控制器的对象。希望这有帮助。

我认为您需要转到调用_error_messages部分的文件,并简单地确保使用以下方法调用它:

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

这是因为您要添加隐藏字段标记。

最新更新