Home
Ruby on Rails
Cheatsheets
def new @customer = Customer.new 3.times do @customer.addresses.build end end
<% form_for(@customer) do |f| %> <%= f.text_field :name %> <%= f.text_field :email %> <% @customer.addresses.each do |address| %> <% fields_for "customer[addresses][]", address do |fields| %> <%= fields.text_field :number %> <%= fields.text_field :street %> <% end %> <% end %>
<form id="new_customer" class="new_customer" method="post" action="/customers"> <input type="text" size="30" name="customer[name]"/> <input type="text" size="30" name="customer[email]"/> <input type="text" size="30" name="customer[addresses][][number]"/> <input type="text" size="30" name="customer[addresses][][street]"/> <input type="text" size="30" name="customer[addresses][][number]"/> <input type="text" size="30" name="customer[addresses][][street]"/> <input type="text" size="30" name="customer[addresses][][number]"/> <input type="text" size="30" name="customer[addresses][][street]"/> <input type="submit" value="Create" name="commit"/> </form>
params = {
"customer" => { "name"=>"David Pettifer",
"email"=>"david.p@dizzy.co.uk",
"addresses"=> [
{ "number"=>"31", "street"=>"High" },
{ "number"=>"22", "street"=>"Brook" },
{ "number"=>"16", "street"=>"Kents" } ]
} }
fields_for creates a scope around a specific model object like form_for, but doesn't create the form tags themselves, making fields_for suitable for specifying additional model objects in the same form. See the example.
<% form_for(@customer, :html => { :multipart => true }) do |f| %> <%= f.file_field :image_file %> <%= submit_tag %> <% end %>
class Customer < ActiveRecord::Base def image_file =(uploaded_data) self.filename = uploaded_data.original_filename self.image_data = uploaded_data.read self.size = uploaded_data.size self.content_type = uploaded_data.content_type end end
f.error_messages_for
f.check_box :terms, { :class => 'check' }, "yes", "no"
f.file_field :image
f.hidden_field :id
f.label :customer, "Text for label"
f.password_field :password
f.radio_button :language, "French"
f.text_area :comment, :size => "20x30", :disabled => "disabled"
f.text_field :age, :size => "20", :class => "age_box"
When standard routes are used in a RESTful context, Rails will reflect upon the object passed to it and automatically build a form with the relevant RESTful URL depending on whether the form is wrapping a new record (create) or an existing record (update). Nested routes will require you to be more verbose. Standard routes
|
Standard routes |
new record? |
method |
URL |
|
|
yes |
POST |
|
|
|
no |
PUT |
|
|
Nested routes |
new record? |
method |
URL |
|
|
yes |
POST |
|
|
|
no |
PUT |
|
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 %>
|
|
: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 |
|
|
ActiveRecord model object |
If the |
|
|
"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 |
|
|
{hash} |
A {hash} of HTML attributes which will be added to the HTML |
|
|
:symbol |
Pass as part of the {hash} of HTML attributes. Can be |
When a form is submitted to a Rails application, the parameters are automatically translated by Rails into the params object which is accessible as a hash structure.
|
|
id=1 |
|
|
|
id=1&color=red |
|
|
|
user[name]=David |
|
|
|
user[address][city]=London |
|
|
|
user[address][street]=Road |
|
[] after the name of a model object, such as address[], will insert the id of the record you are editing into the input field, useful for editing multiple records on one form:|
|
address[4][country]=England |
|
|
|
address[4][town]=London |
|
text_field "address[]", :country text_field "address[]", :town text_field "address[]", :country text_field "address[]", :town |
{ :address => [
{ :country => "England", :town => "London" },
{ :country => "Australia", :town => "Sydney" }
]
}
|
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
3 MONTHS AGO
vinay
Thanks
thanks for this nice article....
ABOUT 1 MONTH AGO
mosquete
Visual representation not necessary
Thanks for the cheatsheet! And here's some feedback: I think the "Visual representation of params" is unnecessary. The description of "params[]" just above it leaves no doubt about the organization of its content. So that space could be used for something else.