Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleCaptcha image not loading (Can't mass-assign protected attributes)

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**
like image 316
SujitS Avatar asked Feb 19 '14 06:02

SujitS


4 Answers

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)

like image 99
Vyacheslav Konovalov Avatar answered Nov 15 '22 04:11

Vyacheslav Konovalov


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

like image 32
wpp Avatar answered Nov 15 '22 03:11

wpp


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
like image 36
HackerKarma Avatar answered Nov 15 '22 02:11

HackerKarma


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?

like image 25
pamit Avatar answered Nov 15 '22 02:11

pamit