0

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
0

1 Answer 1

2

My bet is on you're being bitten by strong_parameters:

From the Devise README: https://github.com/plataformatec/devise#strong-parameters:

When you customize your own views, you may end up adding new attributes to forms. Rails 4 moved the parameter sanitization from the model to the controller, causing Devise to handle this concern at the controller as well.

The docs go on to give this example

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
  end
end

So try adding the configure_permitted_parameters method to your ApplicationController, along with the corresponding filter. And replace the :username key with :ticket_id and see if that solves the problem of your ticket_id being nil.

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a LOT! That pretty much fixed it! Using the code above of the bat gave a method error for some reason, but I googled around a bit and modified it. Solution in original question! Thanks a million!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.