Reputation: 6639
Environment: Rails 3.2.3 with Devise
I would like to add a registration code to the registration form. The registration code is NOT in the users table. There's a separate registration_codes for that. The reason it's in the registration form, is that I would like to compare the code entered by the user filling out the registration form to what's in the registration codes table, and produce a value that will go into the users table.
I am getting an error message:
undefined method `registration_code' for #<User:0x007f91b57b7890>
and it's pointing to the code that displays the registration_code text box in views/registrations/new.html.erb
How do I get around the fact that registration_code is not part of the users table? The form was working fine, until I added the registration_code field to the form.
Upvotes: 0
Views: 86
Reputation: 5508
I assume you're using a form builder to create the form (a form_for
block). In this case, you're probably using something like <%= f.text_field :registration_code %>
to display the text field, but Rails can't find registration_code
on the User model, because, like you said, it's not there.
Instead, try using <%= text_field_tag :registration_code %>
. You can access this from your controller through params[:registration_code]
Upvotes: 1
Reputation: 492
I would create a method in your User model that handles your registration code logic:
def registration_code=(code)
# Check registation_codes table and produce a value that will go into the users table
end
Submitting a form with this new field should now call this method with the submitted value
Upvotes: 1