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.
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.
hey guys i want to register a new user with the help of devise but the view containes only 2 fields which are email and the password
i wish to save name, address, city, etc which are in my users table as well. In the view of registration/new.html.erb
after i added them in the view and i clicked sign up the following errors showed up
5 errors prohibited this user from being saved:
Name can't be blank
City can't be blank
Address can't be blank
Country can't be blank
Postal code can't be blank
so can someone please tell me what do i have to do to make devise accept these additional atrributes aside from email and password and save them in my users table
Add the following code in your ApplicationController to let Devise permit parameters other than email and password.
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
permitted_params = %i(name city address country postal) # returns [:name, :city, :address, :country, :postal]
devise_parameter_sanitizer.permit(:sign_up, keys: permitted_params)
devise_parameter_sanitizer.permit(:account_update, keys: permitted_params)
end
end
This way, you're configuring devise to permit the additional fields on both new account registration and profile updation.
For more, read https://github.com/plataformatec/devise#strong-parameters
I am working with Rails 5
I aded new field username in model User.
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_permitted_parameters
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up).push(:username)
end
end
During registration is displayed error: undefined method `for' for # Did you mean? fork
Trace:
NoMethodError (undefined method `for' for #
Did you mean? fork):
app/controllers/users/registrations_controller.rb:7:in `configure_permitted_parameters'
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.0ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.9ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (118.1ms)
Who can help? How solve this problem?
According to the documentation:
The Parameter Sanitaizer API has changed for Devise 4
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
If you just change the .for to .permit it works as well. For example:
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :username) }
It works in both Rails 4.2.x and Rails 5.0.x
Don't forget devise_parameter_sanitizer.permit(:account_update, keys: [:username])
I think you missed account_update in your controller's configure_permitted_parameters method, you need to follow the devise pattern. Devise has a an account update page. You can find this in views/devise/registrations/edit.html.erb, and your code is also not going to work in the sign_up page, here you specified sign_up page
To update your user table, the minute you submit an update in your users/edit, or if you are submitting a username in the sign_up page you need to follow this devise pattern, to update the database User table. Even if you added a new column to the user table, you would have to add it to the configure_permitted_parameters method. In your case it's username, but you missed account_update as well. You're basically saying that you want to update the username or add the string to username field without following the Devise pattern. Any field you add to the User table should follow this Devise pattern. Also you can specify which page is permitted to update this username. In my example below, i'm using the devise update page. So like I said, even if you added a custom field name to Users table you need to follow this pattern. If you have another page where you need to add username, you would just do the same thing.
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])
devise_parameter_sanitizer.permit(:account_update, keys: [:username])
end
end
Next make sure in your user.rb you have validate username in your User model.
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :username, presence: true
end
To update your user table, the minute you submit an update in your users/edit, or if you are submitting a username in the sign_up page you need to follow this devise pattern, to update the database User table. Even if you added a new column to the user table, you would have to add it to the configure_permitted_parameters method. In your case it's username, but you missed account_update as well. You're basically saying that you want to update the username or add the string to username field without following the Devise pattern. Any field you add to the User table should follow this Devise pattern. Also you can specify which page is permitted to update this username. In my example below, i'm using the devise update page. So like I said, even if you added
class ApplicationController < ActionController::Base
before_action :configure_permitted_paramters, if: :devise_controller?
protected
def configure_permitted_paramters
devise_parameter_sanitizer.permit(:sign_up, keys: [:fullname])
devise_parameter_sanitizer.permit(:account_update, keys: [:fullname,
:phone_number, :description, :email, :password])
end
end
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.