Ruby on rails Rails重定向不断从关联模型中删除数据
我有一个带有create方法的表单,该表单将数据输入到我的“users”表和“user\u details”表中 当我在create方法完成后被重定向时,会有一个“delete”查询从“user\u details”表中删除我的新记录 为什么会出现删除行 显示删除发生的日志Ruby on rails Rails重定向不断从关联模型中删除数据,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有一个带有create方法的表单,该表单将数据输入到我的“users”表和“user\u details”表中 当我在create方法完成后被重定向时,会有一个“delete”查询从“user\u details”表中删除我的新记录 为什么会出现删除行 显示删除发生的日志 Redirected to http://localhost:3000/home Completed 302 Found in 518ms Started GET "/home" for 127.0.0.1 at 201
Redirected to http://localhost:3000/home
Completed 302 Found in 518ms
Started GET "/home" for 127.0.0.1 at 2012-11-20 20:32:25 -0600
Processing by SessionsController#index as HTML
SQL (0.2ms) BEGIN
(0.1ms) COMMIT
User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY id DESC LIMIT 5
UserDetails Load (0.6ms) SELECT `user_details`.* FROM `user_details` WHERE `user_details`.`user_id` = 34 LIMIT 1
SQL (0.1ms) BEGIN
SQL (0.2ms) DELETE FROM `user_details` WHERE `user_details`.`id` = ? [["id", 8]]
(6.0ms) COMMIT
UserDetails Load (0.5ms) SELECT `user_details`.* FROM `user_details` WHERE `user_details`.`user_id` = 1 LIMIT 1
SQL (0.1ms) BEGIN
(0.0ms) COMMIT
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `user_details`.* FROM `user_details` WHERE `user_details`.`user_id` = 1 LIMIT 1
SQL (0.1ms) BEGIN
(0.1ms) COMMIT
Rendered sessions/index.html.erb within layouts/application (0.2ms)
Completed 200 OK in 235ms (Views: 63.6ms | ActiveRecord: 93.7ms)
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company, :user_details_attributes
has_one :user_details, :dependent => :destroy
accepts_nested_attributes_for :user_details
after_initialize :build_user_details
# validates email or username when logging in
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end
class用户[:登录]
#用于通过用户名或电子邮件进行身份验证的虚拟属性
#这是对真正持久化字段(如“用户名”)的补充
属性访问器:登录
#设置模型的可访问(或受保护)属性
属性可访问:电子邮件、:密码、:密码确认、:记住我、:用户名、:登录、:家庭电话、:手机、:工作电话、:生日、:家庭地址、:工作地址、:职位、:公司、:用户详细信息\u属性
有一个:用户详细信息,:dependent=>:destroy
接受用户详细信息的\u嵌套\u属性\u
初始化后:生成用户详细信息
#登录时验证电子邮件或用户名
定义self.find_first_by_auth_条件(典狱长条件)
条件=典狱长_conditions.dup
如果login=conditions.delete(:login)
where(条件)。where([“lower(username)=:value或lower(email)=:value”,{:value=>login.downcase}])
其他的
在哪里(条件)。首先
结束
结束
结束
用户详细信息模型
class UserDetails < ActiveRecord::Base
belongs_to :user
attr_accessible :first_name, :last_name, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company
def full_name
[self.first_name, self.last_name].compact.join(' ')
end
end
classuserdetails
会话控制器
class SessionsController < ApplicationController
layout 'application'
before_filter :authenticate_user!
def index
render :layout => 'application'
end
def new
render :layout => 'login'
end
end
class sessioncontroller“应用程序”
结束
def新
呈现:布局=>“登录”
结束
结束
应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :instantiate_user, :instantiate_message, :recent_users, :authenticate_user!
def instantiate_user
@user = User.new
end
def instantiate_message
@message = Message.new
end
def recent_users
@recents = User.last(5).reverse
end
end
class ApplicationController
初始化后注释:从用户模型生成用户详细信息
解决了我的问题。我之所以添加它,是因为我遇到了这个问题,但这个问题现在也解决了。在初始化后注释:从用户模型生成用户详细信息解决了我的问题。我添加它是因为我遇到了这个问题,但这个问题现在也解决了