我是RoR的新手,並且正在使用rails 3.2.3。Ruby on Rails - 設計確認
我一直在使用設計,迄今爲止它一直很棒,但是,我遇到了問題。
我有一個User
表與devise和一個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
,則角色和用戶數據將正確插入到連接表中。
這是怎麼回事?有小費嗎?
由於提前,
問候
更新
@KyleÇ
我創建與常規行動用戶:
查看:
<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
這是很無奈,別人這個問題激活:confirmable
時想出了用HABTM?
感謝您的幫助
檢查這個【答案】(http://stackoverflow.com/questions/11954270/devise-安裝從現有模型數據庫)完全相同的問題。檢查設計2.0文檔。 – 2012-08-14 15:42:44
我這樣做了(除了:unconfirmed_email),但我的連接表(users_role)中的角色仍未創建。會是什麼呢? – Silver 2012-08-14 16:27:12