Reputation: 693
Im a RoR rookie and am using rails 3.2.3.
I've been using devise and so far it has been great, however, I've run into a problem.
I have a User
table with devise and a HABTM association with a Role table. I have the join table created and everything is fine. When I create a user and choose it's role, it creates the data in the join table correctly.
However, I activated devises' confirmable
option and things started to go wrong.
When I create a new user, it no longer inserts the record in the join table as it should.
I mean, all I have literary done was add , :confirmable
in front of the other devise options such as :database_authenticatable, :recoverable, :rememberable, :trackable
and :validatable
.
When I activated :confirmable
I wrote this migration (which I saw on stack overflow also):
class AddConfirmableToDeviseV < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
It sends the email with the link to confirm, nothing wrong with that, but when I click it, the app breaks as that user does not have a role assigned to it, and that is a must.
And as I said, all I did was add :confirmable
. If I comment it out like this #,:confirmable
in my User model, the role and user data gets inserted in the join table correctly.
What's going on? Any tips?
Thanks in advance,
Regards
@Kyle C
I'm creating the user with the regular actions:
View:
<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%>
Then in my controller:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
(...)
Without :confirmable
, this is enough to enter the data in the join table.
On top of this, I have this in my app controller:
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
If i take this out, the user gets logged in when he clicks the confirmation email, however, the middle join table is still doesn't get the association.
I've browsed the documentation (https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb) but I'm still a rookie and I also didn't find anything that would override my app's initial behaviour.
Is there a way to force the input of the records in my join table after I create the user?
I've tried this:
def create
@user = User.new(params[:user])
@role = Role.find(params[:user][:role_ids])
if @user.save
@user.role << @role
@user.save
AND (wrong thing to do but still without success)
(...)
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
This is really frustrating, anyone else came up with this issue when activating :confirmable
with a HABTM?
Thanks for all your help
Upvotes: 0
Views: 1558
Reputation: 4097
t.confirmable is no longer supported please use this migration
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email
Upvotes: 2