5

The problem is that the simple_captcha is not working properly. The captcha image is not showing. Please help.

controller.rb

if simple_captcha_valid?
  do something
else
  do something else
end

view.html.rb

<div class="captcha">
  <%= show_simple_captcha(label: "", placeholder: "", code_type: "numeric") %>   
</div>

Gemfile

gem 'simple_captcha2', require: 'simple_captcha'

Error

SimpleCaptcha::SimpleCaptchaData Load (1.3ms)  SELECT "simple_captcha_data".* FROM "simple_captcha_data" WHERE "simple_captcha_data"."key" = '******' ORDER BY "simple_captcha_data"."id" ASC LIMIT 1
**WARNING: Can't mass-assign protected attributes for SimpleCaptcha::SimpleCaptchaData: key**
wpp
  • 7,093
  • 4
  • 33
  • 65
SujitS
  • 11,063
  • 3
  • 19
  • 41

4 Answers4

4

I have the same problem (with Rails 3). But resolve it by adding

simple_captcha_gem_dir/lib/simple_captcha/simple_captcha_data.rb

attr_accessible :key, :value

where, in my case, simple_captcha_gem_dir is ~/.rvm/gems/ruby-1.9.3-p484/bundler/gems/simple-captcha-2602bf19a63d
(of course for production you can create a fork instead of edit local gem files)

crabvk
  • 130
  • 1
  • 7
4

Here is another solution:

Put this in an initializer (config/initializers/simple_captcha.rb)

Rails.configuration.to_prepare do
  class SimpleCaptcha::SimpleCaptchaData < ::ActiveRecord::Base
    attr_protected
  end
end

Thanks to @zealot128

wpp
  • 7,093
  • 4
  • 33
  • 65
1

Looking at your call <%= show_simple_captcha(:label => "", :placeholder => "", :code_type => "numeric") %> it seems like you're using controller based implementation of simple_captcha

I don't see your ApplicationController code but make sure the following lines are present in app/controllers/application.rb

ApplicationController < ActionController::Base
  include SimpleCaptcha::ControllerHelpers
end

UPDATE Model's Example:

class User < ActiveRecord::Base
  apply_simple_captcha
end

class User < ActiveRecord::Base
  apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true
end
HackerKarma
  • 620
  • 7
  • 18
  • In which model? And what to add. attr_accessible :[what to write here] and where to write this line, in which model. – SujitS Feb 19 '14 at 07:07
  • I thought you were using Rails 3 version and suggested to add `attr_accessible` and `accepts_nested_attributes_for` in the model. But, seems like you're migrated to Rails 4. It would be great if you could share the model. See my updated answer. – HackerKarma Feb 19 '14 at 23:15
  • Yes the include part is there. Suppose I am implementing captcha for user signup then, where to add attr_accessible, (in user model or any else model). Thank you. – SujitS Feb 20 '14 at 04:35
  • Please see my UPDATE in the answer. Are you using Rails 3 or 4? `attr_accessible` is no longer required in Rails 4 as the strong params were introduced. – HackerKarma Feb 20 '14 at 14:57
1

I had the same problem with Rails 4.1.4, Ruby 2.1.2 I used the config below in simple_captcha.rb

Rails.configuration.to_prepare do
  class SimpleCaptcha::SimpleCaptchaData < ::ActiveRecord::Base
    attr_protected :captcha, :captcha_key
  end
end

but... As I noticed in simple captcha controller in .rvm/gems/bundler, captcha key/value is passed via params[:captcha_key] and params[:captcha] which impose the problem because in view I used

<%= f.simple_captcha :label => "Enter numbers.." %> 

in which params are passed via params[:MODEL][:captcha]

so... for now I just added a set_captcha action in controller to set

params[:captcha] = params[:MODEL][:captcha]
params[:captcha_key] = params[:MODEL][:captcha_key]

I'm looking for a better solution, any idea?

Tim Baas
  • 6,035
  • 5
  • 45
  • 72
pamit
  • 326
  • 2
  • 10