I’m trying to configure devise to validate user registration by checking if a code the user inputs at registration exists in a table. To test it out, I'm currently just testing input against a static string. Any thoughts on how to do it? Problem is that string is not being passed from the :ticket_id registration field to the :invite_code_valid function in the user class for some reason. When I check what's in the variable, it's just blank, and using string "hello" when registering gives the error message I specified.
Code is in hero git so I can’t link to it, but I did this:
Migration:
class AddUserTicketIdField < ActiveRecord::Migration
def change
change_table :users do |t|
t.text :ticket_id
end
end
end
In models/user.rb:
validate :invite_code_valid, :on => :create
def invite_code_valid
unless self.ticket_id == "hello"
self.errors.add(:ticket_id, "membership code is not one we recognize, check again?")
end
end
In devise/registrations/new.html.erb:
<div class="field">
<%= f.label :Membership_code %><br />
<%= f.text_field :ticket_id, autocomplete: "off" %>
</div>
To see if the code works at all I tried validating a certain email like this:
def invite_code_valid
unless self.email == "[email protected]"
self.errors.add(:ticket_id, "membership code is not one we recognize, check again?")
end
end
That works. When registering using email [email protected], registration goes though, but not otherwise.
What is going on, am I missing something about how Devise works?
–––––––––––– SOLVED ––––––––––––
Josh Deedens answer below hit the spot. THANKS! For future reference, this is what I did.
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :ticket_id
end
end