I created user table using devise and then added two more columns in it. Then also added the before_action in the Application Controller:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:email,:password,:usertype])
devise_parameter_sanitizer.permit(:account_update, keys: [:name,:email,:password,:current_password,:usertype])
end
Now the issue is when I sign up it gives me an error. Transaction is committed but I guess the devise doesnt knows the url or something like that
The error I get is like this:
undefined method `user_url' for #<Devise::RegistrationsController:0x0000000000e100>
if options.empty?
recipient.public_send(method, *args)
else
recipient.public_send(method, *args, options)
end
Related
I want to permit :full_name parameter for my user model registration in devise, and I always getting Unpermitted parameter: :full_name as response for Users::RegistrationsController#create action
I have tried several ways as I show you next:
1. Application controller (option 1)
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
case params[:action]
when 'create'
devise_parameter_sanitizer.permit(:sign_up, keys: %i[full_name])
when 'update'
...
end
end
end
Result => Unpermitted parameter: :full_name
2. Registration controller (option 2)
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: :create
protected
def configure_sign_up_params
params.require(:user).permit(%i[full_name])
end
end
Result => Unpermitted parameter: :full_name
3. Registration controller (option 3)
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: :create
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: %i[full_name ])
end
end
Result => Unpermitted parameter: :full_name
In my gemfile:
gem 'devise', '~> 4.8'
In my routes:
devise_controllers = {
confirmations: 'users/confirmations',
registrations: 'users/registrations',
invitations: 'users/invitations',
}
devise_for :users, controllers: devise_controllers
I have read devise strong params but to be honest I do not know what I am doing wrong.
Also I tried to debug in Users::RegistrationsController#create what is happening with:
def create
super do
binding.pry
end
end
but it skips the debugger breakpoint... do you have any idea what is going on?
Cheers!
Edit:
Following suggestion from JohnP, I only left :full_name in devise keys parametter sanitizer for sign_up
Also I removed a callback that is bypassing my debug breakpoint and now I can stop with pry in create action
Generally, you write strong params for a specific controller, not in your ApplicationController, because the permitted conditions will be different for each model. When using devise_parameter_sanitizer, you only need to include the extra fields you're adding - this isn't setting up your strong params from scratch, just adding keys to the default Devise list.
So, you should find that this is all you need in your Users::RegistrationsController.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name])
end
(BTW, ensure you refer to the parameter correctly, as params[:user][:full_name].)
(Oh, and if you want to do debugging, I'd suggest installing the byebug gem. You just add an extra line byebug where you want to have a breakpoint.)
I followed these instructions and now have time_zone as a field when the user signs up.
However, the user cannot edit that field, when I try:
Unpermitted parameter: :time_zone
What I know
Since the approach works on /sign_up, I suspect something has to be done to the before_action to allow the time_zone parameter to work on other pages. e.g. editing / adding to this:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:time_zone])
end
That's my best guess, and I'm not sure how to do it if it is correct.
Also, I'm not sure if :sign_up is referring to a route, a controller method or something else?
Note
Devise docs on strong parameters
What I've tried
Attempt 1
def configure_permitted_parameters
devise_parameter_sanitizer.permit([:sign_up, :update], keys: [:time_zone])
end
2
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:time_zone])
devise_parameter_sanitizer.permit(:update, keys: [:time_zone])
end
3
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:time_zone])
devise_parameter_sanitizer.permit(:edit, keys: [:time_zone])
end
Got it!
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:time_zone])
devise_parameter_sanitizer.permit(:account_update, keys: [:time_zone])
end
That and devise's other methods can be found here: https://github.com/heartcombo/devise#strong-parameters
Hi i tried to implement forest_admin gem in rails 5 application
When i generate install with this command
rails g forest_liana:install <ENVIRONMENT SECRET>
The logs are alias_method': undefined method current_user' for classApplicationController' (NameError)
My aplication Controller
class ApplicationController < ActionController::Base
skip_before_action :verify_authenticity_token
before_action :session_expirada, unless: :devise_controller?
before_action :set_attr_to_current_user, unless: :devise_controller?
layout :layout_by_resource
alias_method :devise_current_user, :current_user
include RedirectFromEmail
# Pundi Authorization filtros
include Pundit
#after_action :verify_authorized, unless: :devise_controller? , #except: :index
#to catch message error Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
end
What happend?
You are trying to alias the method ":devise_current_user" to ":current_user". IN your controller current_user is not defined. If you define current_user in your controller(see below) then you will not get this error.
def current_user
#logic to get a handle on current user goes here
end
Remove alias and just use regular devise current_user which is included by default
After setup devise and simple_token_authentication and configure the
ApplicationController
acts_as_token_authentication_handler_for User
respond_to :html, :json
protect_from_forgery with: :null_session
before_action :configure_permitted_parameters, if: :devise_controller?
protected
#added username to signup
def configure_permitted_parameters
added_attrs = [:name, :username, :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
#Override authenticate_user! to handle html and json separately
def authenticate_user!
if self.request.format.html?
super
elsif self.request.format.json?
if self.request.parameters["controller"].start_with?("devise")
# use the default if session related
super
else
# others
if user_signed_in?
# use the default if already signed in
super
else
redirect_to root_path
end
end
end
end
When click on the User Edit Profile shows:
ArgumentError in Devise::RegistrationsController#edit wrong number of
arguments (given 1, expected 0) Extracted source (around line #20):
the error points to authenticate_user!
def authenticate_user!
if self.request.format.html?
super
elsif self.request.format.json?
in this case cant Override the authenticate_user!?
Or this is related with rails 5.1?
Someone is having this issue with simple_token_authentication?
The error message is telling you that Devise defines a method named authenticate_user!, which takes one argument. You are overriding the method, but your method doesn't take any arguments. At some point, Devise makes the call authenticate_user!(some_arg_here), so you get the error explanation:
Called with one arg...
|
V
wrong number of arguments (given 1, expected 0)
^
|
...but the method def expects 0 args
Here's a simple example of your error:
class Dog
def bark
puts "wuff"
end
end
Dog.new.bark(3)
--output:--
1.rb:2:in `bark': wrong number of arguments
(given 1, expected 0) (ArgumentError)
Here's the fix:
class Dog
def bark(repeat)
puts (["wuff"]*repeat).join(", ")
end
end
Dog.new.bark(3)
--output:--
wuff, wuff, wuff
So you need to define your authenticate_user! method so that it takes one argument. If you don't care about the argument, then name it trash.
This is the error that is displaying
When assigning attributes, you must pass a hash as an argument.
I am trying to pass params through a URL something like this /users/sign_up?account=571917355
registrations_controller.rb
def new
#user = User.new(params[:account])
end
Is devise doing something that isn't allowing this to work? I've searched, but nothing out there seems to help fix the error. I'm not even really sure what this error means.
For Devise new
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up).push(:account)
end
or
devise_parameter_sanitizer.permit(:sign_up, keys: [:account])
More here info
it should be a post request and parameter has to like this
user[:account]
url should like this /sign_up?user[account]=571917355&user[name]=xyz