开发者

Why is this variable in this block for this validation code empty?

开发者 https://www.devze.com 2023-03-12 08:24 出处:网络
I\'m using Devise and I\'m trying to set up an invite code that the user has to input in order to sign up. I have this code in my user model:

I'm using Devise and I'm trying to set up an invite code that the user has to input in order to sign up. I have this code in my user model:

attr_accessor :invite_code

validates_each :invite_code, :on => :create do |record, attr, value|
    record.errors.add attr, "is wrong" unless value == "12345"
end

and this text field:

<%= f.text_field :invite_code %>

The problem is that value is always empty, so the validation is always failing, even if the invitation code is "12345".

I'm guessing this might have to do with the fact that the file that contains the text field is not in the user directory but is in a separate registrations directory (I did this from following this railscast when setting up omniauth). I'm really unsure though. How do I fix this?

Here's more information:

This is in my routes.rb:

devise_for :users, :controllers => { :registrations => 'registrations'}

this is in my users controller:

def new
  @user = User.new
end

def create
  @user = User.new(params[:user])
  if @user.save开发者_C百科!
    redirect_to videos_path
  else
    render :action => 'new'
  end
end

This is in my registrations controller:

def create  
  super  
  session[:omniauth] = nil unless @user.new_record?
end


I believe that value is passed as an array. So..

record.errors.add attr, "is wrong" unless value[0] == "12345"

should work.


So the problem was that in addition to attr_accessor :invite_code, I needed attr_accessible :invite_code as well

0

精彩评论

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