How to add custom devise fields/ Access devise default controller - ruby-on-rails

I'm trying to use Devise gem in my project. That worked, but I still have a problem:
I setup devise in my application and generated the views, but I added an extra field in the database (username). So, the thing is that I need this username, but it's not being saved in the database when I create a new user :/ The problem seems to be in the controller, but I don't know how to access it.
Do devise hide its controllers? Can I access this controller and simply add the field I want in the params, for example? Do I really need to code a new controller that will override the default one? What should I do? and how?

See doc: https://github.com/plataformatec/devise
You have to tell the devise to save extra parameters in the DB.
Your application_controller.rb file look like:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end

Related

JSON API Rails 6 with Devise - SignUp Problems

When i try to use the sign_up method of Devise, i get an internal server error but, after create the user.
My application.rb:
class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session, only: Proc.new { |c| c.request.format.json? }
before_action :configure_permitted_parameters, if: :devise_controller?
respond_to :json
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [ :username ])
end
end
Here the output,
Any ideas? 🤔
I am supplementing this with Doorkeeper, but please do not alter the operation of Devise. I also did not use Warden on my own anywhere on the app.
This issue seems to be the problem you are having:
https://github.com/heartcombo/devise/issues/4603
They suggest clearing the cookies of your browser
this usually happens when you are upgrading a bunch of stuck including
devise in one branch And than you get back to some other branch for
something and you have this newer cookie in your browser. Simple
solution is to clear cookies in browser.
Other answers mention upgrading devise version

Rails - How do you display data from the devise Users table in the View files?

I'm trying to display a simple welcome message to the user when they log in such as, "Welcome, Anthony!".
I created first_name and last_name attributes on the sign up and migrated them to the devise Users table and permitted the new attributes through the ApplicationController with this code,
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :first_name, :last_name])
end
end
This works perfectly fine, however, I want to display the first_name in my navbar in the application.html.erb file, which acts as a welcome message to the user once theyve signed in.
How do I display info from the Devise Users table?
Thanks,
Ant.
(If I need to add anything else then just let me know!)
Once you logged in you should be able to access a current_user object, from which you can get first_name and display in the welcome message in the application layout.

Create user dashboard and add more user attributes to Devise gem

I have used the 'Devise' gem provided by Rails to for user SignUp/Log In. By default it provides 'email', 'password' and 'password confirmation' fields. I wish to add more fields like 'Nickname', 'City', 'Company' etc and then also create a user profile page (Dashboard) which simply displays this information.
Can you please tell me in detail(at least mention the steps) as to how I can achieve this?
As Iceman stated, you can find how to add parameters at https://github.com/plataformatec/devise. You will have to generate the views and edit the forms to include your parameters. Then the "lazy way" is too add the following code in your Application controller.
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
To create a profile page, you can just add an action to one of your existing controllers or one of the devise controllers.

Rails upload a picture using paperclip and devise

I using devise to manage my authentication, and want to add a new page where users can upload a picture , so when they sign in, they will be redirected to their profile where they can upload a picture, how do I go about that? Should I create a new controller for this matter or override devise's controllers and how will my route for this matter look like? help is much appreciated.
Thanks
I am assuming that you have a User model and image as its attachment field
You need add this to your Application Controller:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :configure_permitted_parameters, if: :devise_controller?
def after_sign_in_path_for(resource)
upload_path
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) << :image
end
end
You need to create an upload page and its corresponding route, where the user uploads the image.

Using Strong Parameters With Devise

I have used Devise for authentication.
Went ahead and updated my Users database to include a bio column. Added this and ran the migration so I can see it is there.
Now I want Users to be able to add a bio once they are logged in. Did some research and I see attr_accessible is no more in rails 4 and I should use strong parameters. Also checked out the Devise documentation but couldn't quite find what I am after.
I see they are added to the controller but as I have used Devise I don't have access to the UsersController
How can I add the ability for Users to update the bio field so it saves in the database?
You should add a before_filter in your ApplicationController to do that. Devise docs contains a section explaining this. I took the code below from there:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
In the example, the attribute :username is allowed to be parsed in the sign_up page.

Resources