You are here: Home Solidarrow Ruby on Rails Solidarrow Cheatsheets

* Form Helpers Cheatsheet

Create forms, check boxes, radio buttons, select lists and more using Rails' built-in form helpers.

form_for

form_for is used to easily manipulate HTML forms which are based upon ActiveRecord model objects:

    <%= form_for(:customer, @customer, :url => { :controller => "customers", :action => "create" }, :html => { :multipart => true, :method => :put }) do |f| %>
      <%= f.text_field :age %> 
      <%= text_field "customer", :age %>
      <%= submit_tag %>
    <% end %>

Parameters

:customer required

:symbol or "string"

The name of the model object for all the fields in the form. All input fields will be prefixed with this. Rails will also look for an @instance_variable with the same name which should contain an instance of an existing or new ActiveRecord model object

@customer optional

ActiveRecord model object

If the @instance_variable containing the model object is named differently, you can pass a variable containing the actual model object here

:url optional

"string" or {hash}

The URL to post the form to. Can take an explicit url as a string, or a hash in the same format as url_for

:html optional

{hash}

A {hash} of HTML attributes which will be added to the HTML <form> tag.

:method optional

:symbol

Pass as part of the {hash} of HTML attributes. Can be :put, :post, :get or :delete

 

 

> RELATED ARTICLE

* Quickly debugging form helpers

Sometimes you want to quickly see the output of helper methods, and constantly clicking refresh in your browser then viewing the page source can be tiresome. Instead, use the Rails console to check helpers are doing what you want them to. > More