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.
Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of the need to handle form control naming and its numerous attributes. Rails does away with this complexity by providing view helpers for generating form markup.
A Rails Model is a Ruby class that can add database records (think of whole rows in an Excel table), find particular data you're looking for, update that data, or remove data. These common operations are referred to by the acronym CRUD--Create, Remove, Update, Destroy.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With