0

我只是在7.3.2 name 和 Gravatar阶段关注 Ruby on Rails 3 Tutorials ( Mhartl ) 第 7 章。

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

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

用户.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
4

4 回答 4

1

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

于 2012-07-02T11:13:44.933 回答
0

在最火的时候,我看到你有 attr_accessor :pasword

我认为应该是 :password

主题:

restful 控制器中缺少一些操作,因此无法创建用户。有关 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

编辑:完成宁静的动作

于 2012-07-02T11:26:14.633 回答
0

我有同样的问题。就我而言,我的销毁操作中的“redirect_to”在“posts_path”中缺少一个“s”。这是 post_path Noob,但值得我检查一下。

于 2014-02-22T18:48:08.983 回答
0

您找不到“user/1”的原因是当您通过键入将微帖子添加到示例数据(db/seeds.rb)时

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

你忘记了前面代码的“END”,所以 db/seeds.rb 的全貌是

User.create!(name:  "Example User",
         email: "example@railstutorial.org",
         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
于 2015-07-05T23:29:14.287 回答