[英]Ruby On Rails - Devise - sessions_controller : undefined 'build_resource'
I'm a beginner with Ruby On Rails and I tried all I can but I can't solve my problem... 我是Ruby On Rails的初学者,我已尽力而为,但无法解决我的问题...
I use devise and I want to use recaptcha on the sign_in and the sign_up pages. 我使用devise,我想在sign_in和sign_up页面上使用recaptcha。 So I overwrite the devise sessions and registrations controllers to make my own create method in which I check the validity of the captcha.
因此,我覆盖了devise会话和注册控制器,以创建自己的create方法,在其中检查验证码的有效性。
So, I made like above: 所以,我像上面这样:
In my own registrations_controller.rb: 在我自己的registrations_controller.rb中:
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
build_resource
resource.errors.clear
resource.errors.add(:base, "Wrong captcha, try again.")
render :new
end
end
end
In my own sessions_controller.rb: 在我自己的sessions_controller.rb中:
class SessionsController < Devise::SessionsController
def create
if verify_recaptcha
super
else
build_resource
resource.errors.clear
resource.errors.add(:base, "Wrong captcha, try again.")
render :new
end
end
end
For the sign_up page, it works well, but for the sign_in page I have a problem because the method 'buil_resource' is protected in the Devise sessions controller. 对于sign_up页面,它工作良好,但是对于sign_in页面,我有一个问题,因为方法'buil_resource'在Devise会话控制器中受到保护。 (I read this : Ruby Devise, SessionsController.create, json - getting NameError: undefined 'build_resource'? ) I tried all the solutions I found but none works --> I don't know what to do now...
(我读到这篇文章: Ruby Devise,SessionsController.create,json-getNameError:undefined'build_resource'? )我尝试了所有发现的解决方案,但没有用->我现在不知道该怎么办...
I'll be grateful for any help on this point : by what can I replace this method ? 在这一点上,我将不胜感激:我可以用什么代替这种方法?
---------------------------------------------- EDIT ---------------------------------------------- ----------------------------------------------编辑- -------------------------------------------
I use the code from the new
method of Devise::SessionsController
like above and it works pretty well: 我使用上面的
Devise::SessionsController
new
方法中的代码,并且效果很好:
class SessionsController < Devise::SessionsController
def create
if verify_recaptcha
super
else
self.resource = resource_class.new(sign_in_params)
#flash[:alert] = "Wrong captcha, try again."
resource.errors.clear
resource.errors.add(:base, "Wrong captcha, try again.")
respond_with_navigational(resource, serialize_options(resource)) { render :new }
end
end
end
Now, the only thing is I don't see the error "Wrong captcha, try again"
if I type a wrong captcha. 现在,唯一的问题是
"Wrong captcha, try again"
如果我输入"Wrong captcha, try again"
我不会看到错误"Wrong captcha, try again"
。
I have the solution to use flash[:alert]
like in the commented line but it's not printed the same way so if someone know why I don't see the resource errors, please give it to me ! 我有在注释行中使用
flash[:alert]
的解决方案,但其打印方式不同,因此,如果有人知道为什么我看不到资源错误,请给我! :) :)
You can try this: 您可以尝试以下方法:
class SessionsController < Devise::SessionsController
def create
if verify_recaptcha
super
else
self.resource = resource_class.new(sign_in_params)
resource.valid?
resource.errors.add(:base, "Wrong captcha, try again.")
respond_with_navigational(resource, serialize_options(resource)) { render :new }
end
end
end
My problem was that only captcha was in resource.errors
, but other attributes not, so I'm validate resource by resource.valid?
我的问题是,只有captcha在
resource.errors
,而其他属性不在,所以我要通过resource.valid验证resource.valid?
after creating object and everything is fine now. 创建对象之后,现在一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.