0
votes

I'm a beginner with Ruby On Rails and I tried all I can but I can't solve my problem...

I use devise and I want to use recaptcha on the sign_in and the sign_up pages. So I overwrite the devise sessions and registrations controllers to make my own create method in which I check the validity of the captcha.

So, I made like above:

In my own 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:

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. (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...

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:

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.

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 ! :)

1
You tried putting include Devise::Controllers::InternalHelpers inside your sessions controller, and that didn't work? Because that is supposed to give you access to build_resource outside of the RegistrationsController - JTG
Thanks for your response but I already tried and I get the error : unitialized constant Devise::Controllers::InternalHelpers - Matthieu.P

1 Answers

0
votes

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? after creating object and everything is fine now.