ruby on rails - Customizing devise controller -
i trying change behaviour of 1 devise controller method. here says can running rails generate devise:controller users.
however doing generated commented code bunch of super calls. if don't know super methods do, how supposed edit lines of code want change?
the modification want simple: if there no admin user yet (none role=admin found), user's role set admin, else, normal user. thought in case after_filter solution, did this:
class usercontroller < devise::registrationscontroller after_filter :set_role, only: [:create] protected def set_role admin_user = user.find_by_role(user::admin_role) if admin_user.nil? @user.role = user::admin_role else @user.role = user::default_role end @user.save end end
my routes:
devise_for :users, controllers: { users: "users" }
however, method not being executed. why? can do?
i think need overriding registrations controller:
devise_for :users, :controllers => {:registrations => "registrations"}
then controller:
controllers/registrations_controller.rb
class registrationscontroller < devise::registrationscontroller def create build_resource(sign_up_params) admin_user = user.find_by_role(user::admin_role) if admin_user.nil? resource.role = user::admin_role else resource.role = user::default_role end if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) respond_with resource, :location => after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location => after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource respond_with resource end end end
Comments
Post a Comment