2012-07-02 50 views
0

我只是章-7在7.3.2名稱和的Gravatar舞臺on Rails的3個教程(Mhartl)下面的Ruby。的ActiveRecord :: RecordNotFound在UsersController#顯示

在這裏,我面臨的一個問題,當我在我的瀏覽器,它是開放的說:

ActiveRecord::RecordNotFound in UsersController#show 
Couldn't find User with id=1 
Rails.root: C:/RubyOnRails/MyWorkPlace/sample_app_1 
Application Trace | Framework Trace | Full Trace 
app/controllers/users_controller.rb:5:in `show' 
Request 
Parameters: 
{"id"=>"1"} 
Show session dump 
Show env dump 
Response 
Headers: 
None 

而且我粘貼下面User_controller.rb和user.rb

user.rb:

require 'digest' 

class User < ActiveRecord::Base 

    attr_accessor :pasword 
    attr_accessible :login, 
        :username, 
        :email, 
        :password, 
        :password_confirmation, 
        :remember_me 

    email_regex = /\A[\w+\-.][email protected][a-z\-.]+\.[a-z]+\z/i 

    validates :name, :presence => true, 
        :length => { :maximum => 50 } 


    validates :email, :presence => true, 
         :format => { :with => email_regex }, 
         :uniqueness => { :case_sensitive => false } 

    validates :pasword, :presence  => true, 
         :confirmation => true, 
         :length  => { :within => 6..40 } 

    def self.authenticate(email, submitted_password) 
     user = find_by_email(email) 
     return nil if user.nil? 
     return user if user.has_password?(submitted_password) 
    end 

    before_save :encrypt_password 

    def has_password?(submitted_password) 
     encrypted_password == encrypt(submitted_password) 
    end 

    private 

    def encrypt_password 
     self.salt = make_salt if new_record? 
     self.encrypted_password = encrypt(password) 
    end 

    def encrypt(string) 
     secure_hash("#{salt}--#{string}") 
    end 

    def make_salt 
     secure_hash("#{Time.now.utc}--#{password}") 
    end 

    def secure_hash(string) 
     Digest::SHA2.hexdigest(string) 
    end      

end 

users_controller.rb:

class UsersController < ApplicationController 

    def show 
     @user = User.find(params[:id]) 
     @title = @user.name 
    end 

    def new 
     @title = "Sign up" 
    end 
end 

回答

1

您確定您創建了id = 1的任何用戶嗎? 要檢查,請轉到rails控制檯並使用id爲1的用戶。如果沒有用戶,請創建一個。

+0

非常感謝你爲你的快速回復Aniruth,我只是檢查有沒有用戶ID是說,(找不到用戶使用id = 1) – Dennis

+0

我試過讓新的使用身份證,但!!!! ActiveModel :: MassAssignmentSecurity :: Error:無法批量分配受保護的屬性:名稱 from c:/RubyOnRails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model /mass_assignment_security/sanitizer.rb:48:in'process_removed_attri butes' from c:/RubyOnRails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/ sanitizer.rb:20:在'debug_protected_attri bute_removal' – Dennis

+0

打開控制檯並鍵入:'@user = User.new,@ user.name =「Name you want」等等,@ user.save'這應該保存一個用戶id = 1。你不能指定id,因爲它的一個受保護的屬性由rails自動處理。 – Vikko

0

在firest,我看你有沒有attr_accessor:pasword

我覺得應該是:密碼

Ontopic:

有一些動作在寧靜的控制器丟失,所以它不會是可能的創建一個用戶。 有關RESTful控制器的更多詳細信息,請參閱http://guides.rubyonrails.org/getting_started.html#rest

class UsersController < ApplicationController 


    def show 
    @user = User.find(params[:id]) 
    @title = @user.name 
    end 

    def new 
    @user = User.new #this creates a empty user object to be filled with signup data 
    @title = "Sign up" 
    end 

    def create 
    @user = User.new(params[:user]) #this creates a new user object with the data you entered before. 
    if @user.save #if the data is valid, save it 
     redirect_to user_path(@user) #and go to the @user show action 
    else 
     render :action => :new #edit the invalid user data 
    end 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user]) 
     redirect_to user_url(@user) 
    else 
     render edit_user_url(@user) 
    end 
    end 

    def index 
    @users = User.all 
    end 

    def destroy 
    @user = User.find(params[:id] 
    @user.destroy 
    redirect_to :action => :index 
    end 

end 

編輯:完整的寧靜行動

+0

我糾正了拼寫錯誤,謝謝Vikko。 – Dennis

+0

'validates:pasword,:presence => true,'另一個錯字! – Vikko

0

我有同樣的problema。在我的情況下,我的銷燬行動中的'redirect_to'錯過了'posts_path'中的's'。這是post_path Noob,但值得我檢查。

0

你找不到原因「用戶/ 1」是當你鍵入

users = User.order(:created_at).take(6) 
50.times do 
    content = Faker::Lorem.sentence(5) 
    users.each { |user| user.microposts.create!(content: content) } 
end 

加微柱的樣本數據(DB/seeds.rb)你忘了「END」以前的代碼,所以數據庫的全貌/ seeds.rb是

User.create!(name: "Example User", 
     email: "[email protected]", 
     password:    "foobar", 
     password_confirmation: "foobar", 
     admin:  true, 
     activated: true, 
     activated_at: Time.zone.now) 

99.times do |n| 
    name = Faker::Name.name 
    email = "example-#{n+1}@railstutorial.org" 
    password = "password" 
    User.create!(name: name, 
      email: email, 
      password:    password, 
      password_confirmation: password, 
      activated: true, 
      activated_at: Time.zone.now) 
end 

users = User.order(:created_at).take(6) 
50.times do 
    content = Faker::Lorem.sentence(5) 
    users.each { |user| user.microposts.create!(content: content) } 
end 
相關問題