339 lines
9.4 KiB
HTML
339 lines
9.4 KiB
HTML
<!doctype html>
|
|
<html lang='en' class='no-js '>
|
|
<head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta charset='utf-8'>
|
|
<meta content='width=device-width, initial-scale=1.0' name='viewport'>
|
|
<link href='./assets/favicon.png' rel='shortcut icon'>
|
|
<meta content='/rails-forms.html' name='app:pageurl'>
|
|
|
|
|
|
<title>Form helpers cheatsheet</title>
|
|
<meta content='Form helpers cheatsheet' property='og:title'>
|
|
<meta content='Form helpers cheatsheet' property='twitter:title'>
|
|
<meta content='article' property='og:type'>
|
|
|
|
|
|
|
|
<meta content='https://assets.devhints.io/previews/rails-forms.jpg?t=20201203083858' property='og:image'>
|
|
<meta content='https://assets.devhints.io/previews/rails-forms.jpg?t=20201203083858' property='twitter:image'>
|
|
<meta content='900' property='og:image:width'>
|
|
<meta content='471' property='og:image:height'>
|
|
|
|
|
|
|
|
<meta content="The one-page guide to Form helpers: usage, examples, links, snippets, and more." name="description">
|
|
<meta content="The one-page guide to Form helpers: usage, examples, links, snippets, and more." property="og:description">
|
|
<meta content="The one-page guide to Form helpers: usage, examples, links, snippets, and more." property="twitter:description">
|
|
|
|
|
|
<link rel="canonical" href="https://devhints.io/rails-forms">
|
|
<meta name="og:url" content="https://devhints.io/rails-forms">
|
|
|
|
|
|
|
|
|
|
|
|
<meta content='Devhints.io cheatsheets' property='og:site_name'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta content='Rails' property='article:section'>
|
|
|
|
|
|
|
|
|
|
|
|
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-106902774-1'></script>
|
|
<script>
|
|
|
|
window.dataLayer=window.dataLayer||[];
|
|
function gtag(){dataLayer.push(arguments)};
|
|
gtag('js',new Date());
|
|
gtag('config','UA-106902774-1');
|
|
</script>
|
|
|
|
|
|
|
|
<meta property='page:depth' content='1'>
|
|
|
|
|
|
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
|
|
<script>(function(H){H.className=H.className.replace(/\bNoJs\b/,'WithJs')})(document.documentElement)</script>
|
|
|
|
<script>(function(d,s){if(window.Promise&&[].includes&&Object.assign&&window.Map)return;var js,sc=d.getElementsByTagName(s)[0];js=d.createElement(s);js.src='https://cdn.polyfill.io/v2/polyfill.min.js';sc.parentNode.insertBefore(js, sc);}(document,'script'))</script>
|
|
|
|
<!--[if lt IE 9]><script src='https://cdnjs.cloudflare.com/ajax/libs/nwmatcher/1.2.5/nwmatcher.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js'></script><script src='https://cdn.rawgit.com/gisu/selectivizr/1.0.3/selectivizr.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script><![endif]-->
|
|
|
|
<style>html{opacity:0}</style>
|
|
<link rel="stylesheet" href="./assets/2015/style.css?t=20201203083858">
|
|
<link href="./assets/style.css?t=20201203083858" rel="stylesheet" />
|
|
<link href="./assets/print.css?t=20201203083858" rel="stylesheet" media="print" />
|
|
</head>
|
|
<body>
|
|
<div class='all'>
|
|
|
|
<div class='site-header'>
|
|
<div class='container'>
|
|
This is <a href="."><em>Devhints.io cheatsheets</em></a> — a collection of cheatsheets I've written.
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<script type='application/ld+json'>
|
|
{
|
|
"@context": "http://schema.org",
|
|
"@type": "NewsArticle",
|
|
"mainEntityOfPage": {
|
|
"@type": "WebPage",
|
|
"@id": "https://google.com/article"
|
|
},
|
|
"headline": "Form helpers cheatsheet",
|
|
"image": [ "https://assets.devhints.io/previews/rails-forms.jpg?t=20201203083858" ],
|
|
"description": "The one-page guide to Form helpers: usage, examples, links, snippets, and more."
|
|
}
|
|
</script>
|
|
<script type='application/ld+json'>
|
|
{
|
|
"@context": "http://schema.org",
|
|
"@type": "BreadcrumbList",
|
|
"itemListElement": [{
|
|
"@type": "ListItem",
|
|
"position": 1,
|
|
"item": {
|
|
"@id": "https://devhints.io/#rails",
|
|
"name": "Rails"
|
|
}
|
|
},{
|
|
"@type": "ListItem",
|
|
"position": 2,
|
|
"item": {
|
|
"@id": "https://devhints.io/rails-forms",
|
|
"name": "Form helpers"
|
|
}
|
|
}]
|
|
}
|
|
</script>
|
|
|
|
|
|
<div class='post-list -single -cheatsheet'>
|
|
<div class='post-item'>
|
|
<div class='post-headline -cheatsheet'>
|
|
<p class='prelude'><span></span></p>
|
|
<h1><span>Form helpers</span></h1>
|
|
|
|
<div class='pubbox'>
|
|
<div class='HeadlinePub' role='complementary'>
|
|
<script async src='https://pubsrv.devhints.io/carbon.js?serve=CE7IK5QM&placement=devhintsio&cd=pubsrv.devhints.io/s' id="_carbonads_js"></script>
|
|
<span class='placeholder -one'></span>
|
|
<span class='placeholder -two'></span>
|
|
<span class='placeholder -three'></span>
|
|
<span class='placeholder -four'></span>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class='post-content -cheatsheet'>
|
|
<h2 id="form-builder">Form builder</h2>
|
|
|
|
<pre><code class="language-haml">- form_for @post do |f|
|
|
</code></pre>
|
|
|
|
<p>Field names will be prefixed with <code>post</code> (the class name), and values will be derived from this object (eg, <code>f.text_field :name</code> from <code>@post.name</code>).</p>
|
|
|
|
<h3 id="options">Options</h3>
|
|
|
|
<pre><code class="language-haml">- form_for @post, |
|
|
url: { method: 'put', action: 'create' }, |
|
|
html: { class: 'nifty_form' } |
|
|
do |f|
|
|
</code></pre>
|
|
|
|
<h2 id="fields">Fields</h2>
|
|
|
|
<h3 id="text">Text</h3>
|
|
|
|
<pre><code class="language-rb">f.text_field :title
|
|
f.text_area :body, size: '60x12'
|
|
</code></pre>
|
|
|
|
<h3 id="checkbox">Checkbox</h3>
|
|
|
|
<pre><code class="language-rb">f.check_box :remember_me
|
|
f.label :remember_me, "Remember me"
|
|
</code></pre>
|
|
|
|
<h3 id="radio">Radio</h3>
|
|
|
|
<pre><code class="language-rb">f.radio_button :gender, 'male'
|
|
f.label :gender_male, "Male"
|
|
|
|
f.radio_button :gender, 'female'
|
|
f.label :gender_female, "Female"
|
|
</code></pre>
|
|
|
|
<h3 id="label">Label</h3>
|
|
|
|
<pre><code class="language-rb">f.label :title
|
|
f.label :title, "Title"
|
|
f.label :title, "Title", class: "title"
|
|
f.label(:post, :terms) { "Accept terms" }
|
|
</code></pre>
|
|
|
|
<h3 id="submit-button">Submit button</h3>
|
|
|
|
<pre><code class="language-rb">f.submit "Create"
|
|
</code></pre>
|
|
|
|
<h3 id="hidden-fields">Hidden fields</h3>
|
|
|
|
<pre><code class="language-rb">f.hidden_field :id
|
|
</code></pre>
|
|
|
|
<h2 id="misc">Misc</h2>
|
|
|
|
<h3 id="the-model">The model</h3>
|
|
|
|
<pre><code class="language-ruby">f.object
|
|
</code></pre>
|
|
|
|
<h3 id="fields-for">Fields for</h3>
|
|
|
|
<pre><code class="language-haml">= form_for @post do |f|
|
|
= fields_for :author, @post.author do |ff|
|
|
= ff.text_field :name
|
|
</code></pre>
|
|
|
|
<h3 id="select-dropdowns">Select dropdowns</h3>
|
|
|
|
<pre><code class="language-rb">f.select :city_id, [['Lisbon',1], ['Madrid',2], ...], 4
|
|
# (4 = selected)
|
|
|
|
options_for_select [['Lisbon',1], ['Madrid',2], ...], 4
|
|
# Just makes <option> tags
|
|
</code></pre>
|
|
|
|
<h3 id="collections">Collections</h3>
|
|
|
|
<pre><code>f.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial
|
|
f.collection_select :city_id, City.all, :id, :name
|
|
# (field, collection, value_key, label_key)
|
|
</code></pre>
|
|
|
|
<h3 id="time-select">Time select</h3>
|
|
|
|
<pre><code class="language-rb">f.time_zone_select :time_zone
|
|
f.date_select :birthday
|
|
</code></pre>
|
|
<h3 id="i18n">I18n</h3>
|
|
|
|
<pre><code class="language-yaml">helpers:
|
|
submit:
|
|
# helpers.submit.<action>
|
|
create: "Create a %{model}"
|
|
update: "Confirm changes to %{model}"
|
|
|
|
# helpers.submit.<model>.<action>
|
|
article:
|
|
create: "Publish article"
|
|
update: "Update article"
|
|
|
|
# helpers.label.<model>.<field>
|
|
label:
|
|
post:
|
|
body: "Your body text"
|
|
</code></pre>
|
|
|
|
<h3 id="outside-f">Outside <code>f</code></h3>
|
|
|
|
<pre><code class="language-rb">radio_button("post", "category", "rails")
|
|
radio_button("post", "category", "java")
|
|
|
|
# picks from @post.category
|
|
# <input type="radio" id="post_category_rails" name="post[category]"
|
|
# value="rails" checked="checked" />
|
|
</code></pre>
|
|
|
|
<h3 id="reference">Reference</h3>
|
|
|
|
<pre><code class="language-rb">select(method, choices = nil, options = {}, html_options = {}, &block)
|
|
choices == [ ['label', id], ... ]
|
|
|
|
submit(value=nil, options={})
|
|
</code></pre>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<ul class="social-list ">
|
|
<li class="facebook link hint--bottom" data-hint="Share on Facebook"><a href="https://www.facebook.com/sharer/sharer.php?u=https://devhints.io/rails-forms.html" target="share"><span class="text"></span></a></li>
|
|
<li class="twitter link hint--bottom" data-hint="Share on Twitter"><a href="https://twitter.com/intent/tweet?text=The%20ultimate%20cheatsheet%20for%20Form%20helpers.%20https://devhints.io/rails-forms.html" target="share"><span class="text"></span></a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class="about-the-site">
|
|
<div class="container">
|
|
<p class='blurb'>
|
|
<strong><a href=".">Devhints.io cheatsheets</a></strong> is a collection of cheatsheets I've written over the years.
|
|
Suggestions and corrections? <a href='https://github.com/rstacruz/cheatsheets/issues/907'>Send them in</a>.
|
|
<i class='fleuron'></i>
|
|
I'm <a href="http://ricostacruz.com">Rico Sta. Cruz</a>.
|
|
Check out my <a href="http://ricostacruz.com/til">Today I learned blog</a> for more.
|
|
</p>
|
|
|
|
|
|
<p class='back'>
|
|
<a class='big-button -back -slim' href='.#toc'></a>
|
|
</p>
|
|
|
|
|
|
<p>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/highlight.min.js"></script>
|
|
|
|
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/languages/haml.min.js"></script>
|
|
|
|
|
|
<script src="https://cdn.rawgit.com/rstacruz/unorphan/v1.0.1/index.js"></script>
|
|
<script>hljs.initHighlightingOnLoad()</script>
|
|
<script>unorphan('h1, h2, h3, p, li, .unorphan')</script>
|
|
|
|
</body>
|
|
</html>
|
|
|