ActiveRecord::RecordNotFound in UsersController#show [英] ActiveRecord::RecordNotFound in UsersController#show

查看:36
本文介绍了ActiveRecord::RecordNotFound in UsersController#show的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在 7.3.2 名称和 Gravatar 阶段关注 Ruby on Rails 3 教程 (Mhartl) 第 7 章.

I'm just following Ruby on Rails 3 Tutorials (Mhartl) chapter-7 at the stage of 7.3.2 name and Gravatar.

我在浏览器上打开时遇到一个问题:

Here I am facing a problem when I open on my browser it's says:

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 下面

Also I pasted below User_controller.rb and 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+\-.]+@[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

推荐答案

您确定您创建了任何 id=1 的用户吗?要检查,请转到 rails 控制台并获取 id 为 1 的用户.如果没有用户,则创建一个.

Are you sure you created any user with id=1 ? To check, go to rails console and get the user with id 1. If there is no user, then create one.

这篇关于ActiveRecord::RecordNotFound in UsersController#show的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆