[英]Ruby on Rails - Devise Confirmable
我是RoR新秀,正在使用rails 3.2.3。
我一直在使用devise,到目前为止它很棒,但是,我遇到了一个问题。
我有一个具有devise的User
表和一个与Role表的HABTM关联。 我已经创建了联接表,一切都很好。 当我创建一个用户并选择它的角色时,它会在连接表中正确创建数据。
但是,我激活了装置的可confirmable
选项,结果开始出现问题。 当我创建一个新用户时, 它不再将记录插入联接表中。
我的意思是,我所做的所有工作都是在其他设计选项(如:database_authenticatable, :recoverable, :rememberable, :trackable
和:validatable
)之前添加, :confirmable
。
当我激活:confirmable
我写了这个迁移(我也在堆栈溢出上看到了):
class AddConfirmableToDeviseV < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
它发送带有链接的电子邮件进行确认,没什么错,但是当我单击它时,该应用程序中断,因为该用户没有分配角色,这是必须的。
正如我所说,我所做的只是添加:confirmable
。 如果我在用户模型中将 其注释为#,:confirmable
, 则角色和用户数据将正确插入连接表中。
这是怎么回事? 有小费吗?
提前致谢,
问候
@凯尔C
我正在通过常规操作创建用户:
视图:
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
(...)
<% for role in Role.find(:all) %>
<div class="field">
<%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
<%= role.name %>
</div>
<%end%>
然后在我的控制器中:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
(...)
没有:confirmable
,这足以在联接表中输入数据。
最重要的是,我在我的应用控制器中有这个:
def after_sign_in_path_for(resource)
if current_user.roles.first.id == 1
admin_dashboard_path
elsif current_user.roles.first.id == 2
manage_path
end
end
如果我将其删除,则用户在单击确认电子邮件时便会登录, 但是 ,中间联接表仍未获得关联。
我浏览了文档( https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb ),但我仍然是菜鸟,而且我也没有找到任何可以覆盖我的应用的初始行为。
创建用户后,有没有办法在连接表中强制输入记录?
我已经试过了:
def create
@user = User.new(params[:user])
@role = Role.find(params[:user][:role_ids])
if @user.save
@user.role << @role
@user.save
AND(做错了事,但仍然没有成功)
(...)
if @user.save
query = ActiveRecord::Base.connection.raw_connection.prepare("INSERT INTO roles_users (role_id, user_id) VALUES (?,?);")
query.execute(@role.id, @user.id)
query.close
这真令人沮丧,其他人在用HABTM激活:confirmable
时想到了这个问题?
感谢你的帮助
t.confirmable不再受支持,请使用此迁移
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.