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" } ]
} }
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