我只是章-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
非常感谢你为你的快速回复Aniruth,我只是检查有没有用户ID是说,(找不到用户使用id = 1) – Dennis
我试过让新的使用身份证,但!!!! 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
打开控制台并键入:'@user = User.new,@ user.name =“Name you want”等等,@ user.save'这应该保存一个用户id = 1。你不能指定id,因为它的一个受保护的属性由rails自动处理。 – Vikko