我正在使用Rails 4.2和devise.Request类型将永远是JSON.My注册控制器看起来像这样
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
控制台中显示的参数如下:
Parameters: {"email"=>"test123@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "registration"=>{"email"=>"test123@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
它产生的错误是控制器无法识别电子邮件和密码字段,并始终返回错误,如:
["Email can't be blank","Password can't be blank"]
这是否因为我在代码中写的任何愚蠢而发生?请帮助
答案 0 :(得分:-1)
你的configure_sign_up_params
方法必须是这样的。
def configure_sign_up_params
devise_parameter_sanitizer.for(:registration) do |u|
u.permit(:email, :password, :password_confirmation)
end
end
如果您可以看到控制台内容已发布的内容,则其中包含一个以registration
为主键的哈希:
"registration"=>{"email"=>"test123@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}