ruby-on-rails-3formtastic

How to add an input not related to the model with Formtastic


For a subscription form, I need to have a field that is not mapped to my User object.

This field is only used to ask the user for an invitation code, that is checked against an invitations list in the create action of the controller, but not saved anywhere, and without a relation to this User object.

I tried :

<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= f.input :invitation_key, :input_html => {:name => 'invitation_key', :id => 'invitation_key'} %>

But it does not work.

What is the correct way to do this with Formtastic ?


Solution

  • Melinda's answer is fine, but since the extra attribute is somewhat related to the User model, I would create an accessor on the User (actually, I'd create a Signup model, but that's a digression):

    class User
      attr_accessor :invitation_key
    end
    

    Then modify your controller to either check @user.invitation_key or params[:user][:invitation_key].