Ruby on rails 3 ActiveRecord::在UsersController#show中找不到RecordNot
我只是在学习RubyonRails 3教程(Mhartl)第7章的7.3.2 name and Gravatar 在这里,当我在浏览器上打开时,我遇到了一个问题,上面写着:Ruby on rails 3 ActiveRecord::在UsersController#show中找不到RecordNot,ruby-on-rails-3,Ruby On Rails 3,我只是在学习RubyonRails 3教程(Mhartl)第7章的7.3.2 name and Gravatar 在这里,当我在浏览器上打开时,我遇到了一个问题,上面写着: ActiveRecord::RecordNotFound in UsersController#show Couldn't find User with id=1 Rails.root: C:/RubyOnRails/MyWorkPlace/sample_app_1 Application Trace | Framework
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+\-.]+@[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
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
def new
@title = "Sign up"
end
end
需要“摘要”
类用户true,
:length=>{:max=>50}
验证:email,:presence=>true,
:format=>{:with=>email_regex},
:唯一性=>{:区分大小写=>false}
验证:pasword,:presence=>true,
:confirmation=>true,
:length=>{:within=>6..40}
def自我验证(电子邮件、提交的密码)
用户=通过电子邮件查找(电子邮件)
如果user.nil,则返回nil?
如果用户有密码,则返回用户?(已提交密码)
结束
保存前:加密密码
def有密码?(已提交密码)
加密密码==加密(已提交密码)
结束
私有的
def加密密码
self.salt=如果有新记录,则制作盐?
self.encrypted_password=加密(密码)
结束
def加密(字符串)
安全散列(“#{salt}--#{string}”)
结束
def制盐
安全散列(“#{Time.now.utc}--#{password}”)
结束
def secure_散列(字符串)
摘要::SHA2.hexdigest(字符串)
结束
结束
用户\u控制器.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
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
def new
@title = "Sign up"
end
end
class UsersController
是否确实创建了id为1的用户?
要进行检查,请转到rails控制台并获取id为1的用户。如果没有用户,那么创建一个。在firest,我看到您有attr\u访问器:pasword 我想应该是:密码 本体论: restful控制器中缺少一些操作,因此无法创建用户。 有关RESTful控制器的更多详细信息,请参阅
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
class UsersController:new#编辑无效的用户数据
结束
结束
定义编辑
@user=user.find(参数[:id])
结束
def更新
@user=user.find(参数[:id])
如果@user.update_属性(参数[:user])
将\重定向到用户\ url(@user)
其他的
呈现编辑用户url(@user)
结束
结束
def索引
@users=User.all
结束
def销毁
@user=user.find(参数[:id]
@用户破坏
将_重定向到:action=>:index
结束
结束
编辑:完成restful操作我也遇到了同样的问题。在我的例子中,我的detroy操作上的“重定向到”在“posts\u path”中丢失了一个。这是post\u path Noob,但值得我检查一下。你找不到“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
非常感谢您的快速回复Aniruth,我刚刚检查了一下,上面说没有用户id(找不到id=1的用户)我尝试创建新的旧id,但是!!!!ActiveModel::MassAssignmentSecurity::Error:无法批量分配受保护的属性:c中的名称:/RubyOnRails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/ActiveModel-3.2.3/lib/active\u model/mass\u assignment\u security/sanitizer.rb:48:in
进程中\u从c中删除了属性:/RubyOnRails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active\u model/mass\u assignment\u security/sanitizer.rb:20:in
debug\u protected\u attri bute\u removation'打开控制台并键入:`@user=user.new,@user.name=“name you want”等等,@user.save`应该保存id=1的用户。您不能指定id,因为它是rails保护的属性,将自动处理。是的,我尝试过(ActiveModel::MassAssignmentSecurity::Error:can't mass assign protected attributes es:name)`validates:pasword,:presence=>true,`另一个输入错误!