【问题标题】:Rails Tutorial 3, RSpec FailuresRails 教程 3,RSpec 失败
【发布时间】:2011-06-11 01:15:44
【问题描述】:

我正在浏览 Ruby on Rails 教程 3,并且正在做这件事,但我刚刚遇到了一些没有得到解决的问题。当我运行我的规范时,两个测试失败了。

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

非常感谢任何关于我应该看哪里的想法。

【问题讨论】:

  • 您的错误是否来自您的 _error_messages.html.erb:3 模板?
  • 可以把app/views/shared/_error_messages.html.erb的内容发一下吗?
  • 非常感谢你们!你为我指明了正确的方向。问题不在error_messages.html.erb;它在我的edit.html.erb 中,我应该在这里创建它抱怨的“对象”变量。你是最棒的!

标签: ruby-on-rails ruby-on-rails-3 rspec-rails


【解决方案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

【讨论】:

  • 感谢您及时了解本教程,更感谢您对我的更新进行评价。我很感激。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多