user3247042 user3247042 - 1 month ago 5x
Ruby Question

Rails, use a form field to set something on the user registration (devise)

I would really like to add a form field "invite_code" to the user sign up form, but I don't know how to add the invite_code to the controller so that the application knows how to look for it?

The form in the sign up on the template would read:

<% form_for do |f| %>
<span>Email:</span> <% %><br>
<span>Name:</span> <% %><br>
<span>Invite Code:</span> <% f.invite_code %><br>
<% end %>

The "invite_code" isn't part of the database or anything, but in the user registration model, I want to put a:

before_save :invite_promo
def invite_promo
if @invite_code.present? && @invite_code == "special_person"
self.special_key = true

Is there an easy way to look for form fields in the template using the model or controller?

So sorry...I'm new to Rails. Thank you so much in advance!

dkp dkp

You need to define a virtual attribute invite_code in User model:

attr_accessor :invite_code

Your form should look as follows:

<%= form_for do |f| %>
   <span>Email:</span> <%= f.email_field :email %><br>
   <span>Name:</span> <%= f.text_field :name %><br>
   <span>Invite Code:</span> <%= f.text_field :invite_code %><br>
<% end %>