Rails 3 / Form without Model: How do I create a form that is not tied to a model?

I have a model, and I have a view that displays a form for creating a new object based on that model. Let's call that form, Form1.

Once the user submits Form1, the object is created. I then want to display, on the following page, a second form Form2, which asks the user to check off various options before the object is saved to the database.

My problem is probably extremely basic. I don't know how to create Form2, given that it is not tied directly to the model. Because I am a Rails newbie, I have only created forms as following:

form_for(@object) { |f| ... }

@object is an object instantiated from the model

Problem: I do not believe that kind of code will work for my purposes here. How do I create Form2, given that it must not be based on @object or @object's model?

Some specifics from my app:

The site accepts values (Form1) before redirecting to an OAuth server.

When the user verifies her credentials on the OAuth server, she is redirected back to my site. An XML-RPC request then retrieves information about the user's account on the OAuth server.

The XML response may indicate that the user has only one account on the OAuth server. If so, some values are retrieved from the XML and added to the object--which is then (finally) saved in the database--and the user is redirected to a success page.

However, if the XML response indicates that the user has multiple accounts on the OAuth server, I want to display a form (Form2) that allows the user to select which accounts on the OAuth server to associate with my site. So Form2 really asks the user how many objects to create, rather than about specific attributes of an object.


Solution 1:

Use form_tag instead of form_for, then use the appropriate form helpers: text_field_tag instead of f.text_field, text_area_tag instead of f.text_area, etc. Example:

<%= form_tag "/my_controller/update2" do %>
  <%= text_field_tag "account", "default info" %>
  <%= submit_tag "Save" %>
<% end %>

The Rails API site has a great reference to all of the _tag helpers: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

Solution 2:

Starting in rails3, validations have been decoupled from ActiveRecord so you can create vanilla objects that can be used as validators with the form helpers:

 class Person
   include ActiveModel::Validations

   attr_accessor :first_name, :last_name

   validates_each :first_name, :last_name do |record, attr, value|
     record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
   end
 end

and then in your template:

<%= form_for(Person.new) do |f| %>
...

It's a handy way to get the benefits of the form helpers for things like email forms without having to create a model object tied to your schema.

Solution 3:

To create a table-less model,

class Person
  include ActiveModel::Model

  attr_accessor :first_name, :last_name

  validates :first_name, :last_name, presence: true
end

Then in your view,

<%= form_for(Person.new) do |f| %>
 .... your form ....
<% end %>

Another similar solution can be found at RailsCasts: Active-Model

Solution 4:

If you are looking to use SimpleForm then there is a specific answer for that here Does form_tag work with Simple_form?