Ruby on rails 哈特尔';s教程“;不能';找不到id为1的用户;(第7章)
哈特尔教程中的下一个新手问题。现在在第7章。当我运行应用程序时,我的浏览器中会出现如下内容:Ruby on rails 哈特尔';s教程“;不能';找不到id为1的用户;(第7章),ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,哈特尔教程中的下一个新手问题。现在在第7章。当我运行应用程序时,我的浏览器中会出现如下内容: ActiveRecord::RecordNotFound in UsersController#show Couldn't find User with id=1 Rails.root: C:/rails_project/my_app Application Trace | Framework Trace | Full Trace app/controllers/users_controller.
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=1
Rails.root: C:/rails_project/my_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:4:in `show'
Request
Parameters:
{"id"=>"1"}
Show session dump
Show env dump
Response
Headers:
None
我的用户_controller.rb如下所示:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
describe "profile page" do
#Code to make a user variable
before { visit user_path(user) }
it { should have_selector('h1', :text => user.name) }
it { should have_selector('title', :text => user.name) }
end
end
def new
end
end
class UsersControlleruser.name)}
它{应该有_选择器('title',:text=>user.name)}
结束
结束
def新
结束
结束
谢谢你的帮助和关心 您可以使用
find\u by\u id
而不是find
find
在模型中不存在参数时会导致错误
另请参见:您确定数据库中有用户吗?请叫我lame,但我如何检查它?试试这个:看看您的sqlite数据库中有什么可能我遗漏了什么,但您是否将测试代码与控制器代码放在一起了?我一直理解测试代码放在一个单独的文件中。你检查过你是否有用户了吗?另一种方法是进入命令行,键入
rails c--sandbox
,后跟User.find(1)