开发者

Rails/Devise custom registration validation works in model (Rspec) but not view (Cucumber)

开发者 https://www.devze.com 2023-02-24 06:22 出处:网络
A 开发者_如何学Csimilar question was asked elsewhere on this site (Ruby on rails: Devise, want to add invite code?), and while that solution worked for them, I can\'t get it to work for me to save my

A 开发者_如何学Csimilar question was asked elsewhere on this site (Ruby on rails: Devise, want to add invite code?), and while that solution worked for them, I can't get it to work for me to save my life. My model code:


class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :trackable, :validatable

attr_accessible :email, :name, :password, :password_confirmation attr_accessor :security_code

validate :correct_security_code, :on => :create

protected

def correct_security_code errors.add(:security_code, "Security code not valid") if security_code != "1234" end end

I have tests in both RSpec and Cucumber. The RSpec tests work wonderfully, indicating that all is well validation-wise. The Cucumber/view, however, inevitably fails, calling a validation error on the security code. My view:


= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
  %p
    = f.label :email
    = f.text_field :email
    = f.error :email
  %p
    = f.label :name
    = f.text_field :name
    = f.error :name
  %p
    = f.label :password
    = f.password_field :password
    = f.error :password
    %br/
    = f.hint 'Minimum 4 characters'
  %p
    = f.label :password_confirmation
    = f.password_field :password_confirmation
    = f.error :password_confirmation
  %p
    = f.label :security_code
    = f.text_field :security_code
    = f.error :security_code
  = f.submit 'Sign up', :disable_with => "Submitting..."

This is driving me nuts, any help would be greatly appreciated!

Edit:

Found a fix thanks to the Devise Google Group. It turns out :security_code must be added to both the attr_accessible list as well as the attr_accessor list.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号