Hartl的教程“无法找到id = 1的用户”(第7章)

时间:2012-08-09 21:10:11

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

Hartl教程中的新手问题。现在在第7章。当我正在运行App时,我在浏览器中收到类似的内容:

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

我的user_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

感谢您的帮助&amp; intrest!

1 个答案:

答案 0 :(得分:0)

您可以使用find_by_id代替find

当参数在模型中不存在时,

find会导致错误。

另请参阅:difference between find, where and find_by_id