Ruby on rails 4 Rails 4.0和Desive的强大参数
我正在使用Rails 4.2和Desive。请求类型将始终为JSON。我的注册控制器如下所示Ruby on rails 4 Rails 4.0和Desive的强大参数,ruby-on-rails-4,devise,strong-parameters,Ruby On Rails 4,Devise,Strong Parameters,我正在使用Rails 4.2和Desive。请求类型将始终为JSON。我的注册控制器如下所示 class Users::RegistrationsController < Devise::RegistrationsController before_filter :configure_sign_up_params, only: [:create] def create if request.format(request) == "application/json"
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_sign_up_params, only: [:create]
def create
if request.format(request) == "application/json"
build_resource(sign_up_params)
if resource.save
render :json => {:success => "true", :message => "Thank You For Registering."}
else
render :json => {:success => "false", :message => resource.errors.full_messages}
end
else
super
end
end
protected
def configure_sign_up_params
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:email, :password, :password_confirmation)
end
end
end
它产生的错误是控制器无法识别电子邮件和密码字段,并且总是返回错误,如:
["Email can't be blank","Password can't be blank"]
发生这种情况是因为我在代码中写的愚蠢吗?请帮助您的
配置\u注册\u参数
方法必须是这样才能工作的
def configure_sign_up_params
devise_parameter_sanitizer.for(:registration) do |u|
u.permit(:email, :password, :password_confirmation)
end
end
如果您可以看到您发布的控制台内容,则它包含一个散列,其中包含注册
作为密钥:
"registration"=>{"email"=>"test123@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
"registration"=>{"email"=>"test123@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}