开发者

Rails + Devise + Add User

开发者 https://www.devze.com 2023-03-31 15:01 出处:网络
I have a problem with my Rails app login with Devise. I have the devise user for login and i create a new scaffold user to manage the user.

I have a problem with my Rails app login with Devise.

I have the devise user for login and i create a new scaffold user to manage the user.

my routes.rb are like :

devise :users
resources :users

The problem is when i add a new user when i'm logged with an existent user, the data is not saved in the DB.

if i add a new user when i'm loged out, the user are added to the DB.

to add the new user i use this link : new_user_path. So thats mean it use the resources users

my users_controller

def new
    @user = User.new
end

def create
 @user = User.new(params[:user])
 if @user.save
  redirect_to users_url, :notice => "User created."
 else
   render :action => 'new'
 end

end

in my application_controller i added this functionnality

def after_sign_in_path_for(resource)
company_id = resource.company_id
@company = Company.find(company_id)
puts @company.subdomain
root_url(:host => @company.subdomain + "." + request.domain + '/companies/' + @company.id.to_s)
end

when i add the users when i'm loged in i have that in the server log

Processing by Devise::RegistrationsController#create as HTML
 Parameters: {"commit"=>"Create User", "authenticity_token"=>"6p7qqcu3x8WAJ7eWdtwJLeJQV1lj9IXtomtzxUpTx3k=", "utf8"=>"✓", "user"=>{"usersubtype_id"=>"", "username"=>"3423", "last_name"=>"", "company_id"=>"1", "password"=>"[FILTERED]", "email"=>"foo@bar.com", "first_name"=>"", "usertype_id"=>""}}
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1

i dont know why that works when i'm not logged, but not when i'm logged.

Thanks for your ideas.

EDIT : I checked for devise :registerable in my user model. here is what i have in my model.

devise :database_authenticatable, :registerable,
     :recover开发者_开发技巧able, :rememberable, :trackable, :validatable

 belongs_to :company
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :phone, :owner, :usertype_id, :usersubtype_id, :company_id


This happens because your routes are both pointing to the exactly same place. In this case, both points to the Devise::RegistrationsController from Devise.

What you have to do is just say in your routes that you want to point your Users scaffold to a specific controller.

Go to console and type rake routes and see how they are showing to you and then, change your routes to:

devise :users
resources :users, :controller => "users"

and type again rake routesand see the difference between both.

Let me know if this helped you.


Do you call devise(:registerable) in your user model? If so, you may want to use new_user_registration_path instead of new_user_path. I'm not sure if this will solve your problem, but it could be a step in the right direction.


If using default devise form was the only problem for you, you should have generated devise views with

'rails generate devise:views'

and then find the form inside, views/devise/registrations/new.html.erb and edit it.

You shouldnt have generated scaffold for a user,instead you could have either inherited devise registration controller or created only a User controller.

Scaffold creats controller,model,as well as a migration.Do you need two databases to manage users?

0

精彩评论

暂无评论...
验证码 换一张
取 消