CINXE.COM
Action View Form Helpers — Ruby on Rails Guides
<!doctype html> <html dir="ltr" lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Action View Form Helpers — Ruby on Rails Guides</title> <link rel="stylesheet" type="text/css" href="stylesheets/style-6fc5bf25e695f363fd1dd3d9dbb2f997.css" data-turbo-track="reload"> <link rel="stylesheet" type="text/css" href="stylesheets/print-a87ee66d50ce96bb83ac082f1249fe3e.css" media="print"> <link rel="stylesheet" type="text/css" href="stylesheets/highlight-2794201d063bd2e4dbd0f0874c2a3f6f.css" data-turbo-track="reload"> <link rel="icon" href="images/favicon.ico" sizes="any"> <link rel="apple-touch-icon" href="images/icon.png"> <link rel="canonical" href="https://guides.rubyonrails.org/form_helpers.html"> <script src="javascripts/@hotwired--turbo-764f59c7edbeb902a9068c0340dd274e.js" data-turbo-track="reload"></script> <script src="javascripts/clipboard-8b7aed6f069f0cf58eeae353cd2f898b.js" data-turbo-track="reload"></script> <script src="javascripts/guides-751b87e159daf790ddf7e8e88ad8465a.js" data-turbo-track="reload"></script> <meta property="og:title" content="Action View Form Helpers — Ruby on Rails Guides" /> <meta name="description" content="Action View Form HelpersForms are a common interface for user input in web applications. However, form markup can be tedious to write and maintain because of the need to handle form controls, naming, and attributes. Rails simplifies this by providing view helpers, which are methods that output HTML form markup. This guide will help you understand the different helper methods and when to use each.After reading this guide, you will know: How to create basic forms, such as a search form. How to work with model-based forms for creating and editing specific database records. How to generate select boxes from multiple types of data. What date and time helpers Rails provides. What makes a file upload form different. How to post forms to external resources and specify setting an authenticity_token. How to build complex forms." /> <meta property="og:description" content="Action View Form HelpersForms are a common interface for user input in web applications. However, form markup can be tedious to write and maintain because of the need to handle form controls, naming, and attributes. Rails simplifies this by providing view helpers, which are methods that output HTML form markup. This guide will help you understand the different helper methods and when to use each.After reading this guide, you will know: How to create basic forms, such as a search form. How to work with model-based forms for creating and editing specific database records. How to generate select boxes from multiple types of data. What date and time helpers Rails provides. What makes a file upload form different. How to post forms to external resources and specify setting an authenticity_token. How to build complex forms." /> <meta property="og:locale" content="en_US" /> <meta property="og:site_name" content="Ruby on Rails Guides" /> <meta property="og:image" content="https://avatars.githubusercontent.com/u/4223" /> <meta property="og:type" content="website" /> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet"> <meta name="theme-color" content="#C81418"> </head> <body dir="ltr" class="guide no-js"> <script> document.body.classList.remove('no-js') </script> <a id="main-skip-link" href="#main" class="skip-link" data-turbo="false"> Skip to main content </a> <div id="mobile-navigation-bar"> <div class="wrapper"> <strong class="more-info-label">More at <a href="https://rubyonrails.org/">rubyonrails.org:</a> </strong> <button type="button" class="js-only red-button more-info-button" id="more-info" aria-controls="more-info-links" aria-expanded="false"> More Ruby on Rails </button> <ul id="more-info-links" class="more-info-links hidden"> <li class="more-info"><a href="https://rubyonrails.org/blog">Blog</a></li> <li class="more-info"><a href="https://guides.rubyonrails.org/">Guides</a></li> <li class="more-info"><a href="https://api.rubyonrails.org/">API</a></li> <li class="more-info"><a href="https://discuss.rubyonrails.org/">Forum</a></li> <li class="more-info"><a href="https://github.com/rails/rails">Contribute on GitHub</a></li> </ul> </div> </div> <header id="page-header"> <div class="wrapper clearfix"> <nav id="feature-nav"> <div class="header-logo"> <a href="index.html" title="Guides home for v8.0.1 Guides">Guides</a> <span id="version-switcher" class="js-only"> <label for="version-switcher-select">Version: <span class="visibly-hidden">pick from the list to go to that Rails version's guides</span></label> <select id="version-switcher-select" class="guides-version"> <option value="https://edgeguides.rubyonrails.org/">Edge</option> <option value="https://guides.rubyonrails.org/v8.0/form_helpers.html" selected>8.0</option> <option value="https://guides.rubyonrails.org/v7.2/form_helpers.html">7.2</option> <option value="https://guides.rubyonrails.org/v7.1/form_helpers.html">7.1</option> <option value="https://guides.rubyonrails.org/v7.0/form_helpers.html">7.0</option> <option value="https://guides.rubyonrails.org/v6.1/form_helpers.html">6.1</option> <option value="https://guides.rubyonrails.org/v6.0/form_helpers.html">6.0</option> <option value="https://guides.rubyonrails.org/v5.2/form_helpers.html">5.2</option> <option value="https://guides.rubyonrails.org/v5.1/form_helpers.html">5.1</option> <option value="https://guides.rubyonrails.org/v5.0/form_helpers.html">5.0</option> <option value="https://guides.rubyonrails.org/v4.2/form_helpers.html">4.2</option> <option value="https://guides.rubyonrails.org/v4.1/form_helpers.html">4.1</option> <option value="https://guides.rubyonrails.org/v4.0/form_helpers.html">4.0</option> <option value="https://guides.rubyonrails.org/v3.2/form_helpers.html">3.2</option> <option value="https://guides.rubyonrails.org/v3.1/form_helpers.html">3.1</option> <option value="https://guides.rubyonrails.org/v3.0/form_helpers.html">3.0</option> <option value="https://guides.rubyonrails.org/v2.3/form_helpers.html">2.3</option> </select> </span> </div> <ul class="nav"> <li><a class="nav-item" id="home_nav" href="https://rubyonrails.org/">Home</a></li> <li class="guides-index guides-index-large"> <a href="index.html" id="guides-menu-button" data-aria-controls="guides" data-aria-expanded="false" class="guides-index-item nav-item">Guides Index</a> <div id="guides" class="clearfix" style="display: none;"> <hr /> <dl class="guides-section-container"> <div class="guides-section"> <dt>Start Here</dt> <dd><a href="getting_started.html">Getting Started with Rails</a></dd> <dd><a href="install_ruby_on_rails.html">Install Ruby on Rails</a></dd> </div> <div class="guides-section"> <dt>Models</dt> <dd><a href="active_record_basics.html">Active Record Basics</a></dd> <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd> <dd><a href="active_record_validations.html">Active Record Validations</a></dd> <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd> <dd><a href="association_basics.html">Active Record Associations</a></dd> <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd> <dd><a href="active_model_basics.html">Active Model Basics</a></dd> </div> <div class="guides-section"> <dt>Views</dt> <dd><a href="action_view_overview.html">Action View Overview</a></dd> <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd> <dd><a href="action_view_helpers.html">Action View Helpers</a></dd> <dd><a href="form_helpers.html">Action View Form Helpers</a></dd> </div> <div class="guides-section"> <dt>Controllers</dt> <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd> <dd><a href="action_controller_advanced_topics.html">Action Controller Advanced Topics</a></dd> <dd><a href="routing.html">Rails Routing from the Outside In</a></dd> </div> <div class="guides-section"> <dt>Other Components</dt> <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd> <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd> <dd><a href="action_mailbox_basics.html">Action Mailbox Basics</a></dd> <dd><a href="action_text_overview.html">Action Text Overview</a></dd> <dd><a href="active_job_basics.html">Active Job Basics</a></dd> <dd><a href="active_storage_overview.html">Active Storage Overview</a></dd> <dd><a href="action_cable_overview.html">Action Cable Overview</a></dd> </div> <div class="guides-section"> <dt>Digging Deeper</dt> <dd><a href="i18n.html">Rails Internationalization (I18n) API</a></dd> <dd><a href="testing.html">Testing Rails Applications</a></dd> <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd> <dd><a href="configuring.html">Configuring Rails Applications</a></dd> <dd><a href="command_line.html">The Rails Command Line</a></dd> <dd><a href="asset_pipeline.html">The Asset Pipeline</a></dd> <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd> <dd><a href="autoloading_and_reloading_constants.html">Autoloading and Reloading</a></dd> <dd><a href="api_app.html">Using Rails for API-only Applications</a></dd> </div> <div class="guides-section"> <dt>Going to Production</dt> <dd><a href="tuning_performance_for_deployment.html">Tuning Performance for Deployment</a></dd> <dd><a href="caching_with_rails.html">Caching with Rails: An Overview</a></dd> <dd><a href="security.html">Securing Rails Applications</a></dd> <dd><a href="error_reporting.html">Error Reporting in Rails Applications</a></dd> </div> <div class="guides-section"> <dt>Advanced Active Record</dt> <dd><a href="active_record_multiple_databases.html">Multiple Databases</a></dd> <dd><a href="active_record_composite_primary_keys.html">Composite Primary Keys</a></dd> </div> <div class="guides-section"> <dt>Extending Rails</dt> <dd><a href="rails_on_rack.html">Rails on Rack</a></dd> <dd><a href="generators.html">Creating and Customizing Rails Generators & Templates</a></dd> </div> <div class="guides-section"> <dt>Contributing</dt> <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd> <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd> <dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd> <dd><a href="development_dependencies_install.html">Installing Rails Core Development Dependencies</a></dd> </div> <div class="guides-section"> <dt>Policies</dt> <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd> </div> <div class="guides-section"> <dt>Release Notes</dt> <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd> <dd><a href="8_0_release_notes.html">Version 8.0 - November 2024</a></dd> <dd><a href="7_2_release_notes.html">Version 7.2 - August 2024</a></dd> <dd><a href="7_1_release_notes.html">Version 7.1 - October 2023</a></dd> <dd><a href="7_0_release_notes.html">Version 7.0 - December 2021</a></dd> <dd><a href="6_1_release_notes.html">Version 6.1 - December 2020</a></dd> <dd><a href="6_0_release_notes.html">Version 6.0 - August 2019</a></dd> <dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd> <dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd> <dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd> <dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd> <dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd> <dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd> <dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd> <dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd> <dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd> <dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd> <dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd> </div> </dl> </div> </li> <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li> <li class="guides-index guides-index-small js-only"> <label for="guides-selector"> Navigate to a guide: </label> <select id="guides-selector" class="guides-index-item nav-item"> <option value="index.html">Guides Index</option> <optgroup label="Start Here"> <option value="getting_started.html">Getting Started with Rails</option> <option value="install_ruby_on_rails.html">Install Ruby on Rails</option> </optgroup> <optgroup label="Models"> <option value="active_record_basics.html">Active Record Basics</option> <option value="active_record_migrations.html">Active Record Migrations</option> <option value="active_record_validations.html">Active Record Validations</option> <option value="active_record_callbacks.html">Active Record Callbacks</option> <option value="association_basics.html">Active Record Associations</option> <option value="active_record_querying.html">Active Record Query Interface</option> <option value="active_model_basics.html">Active Model Basics</option> </optgroup> <optgroup label="Views"> <option value="action_view_overview.html">Action View Overview</option> <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option> <option value="action_view_helpers.html">Action View Helpers</option> <option value="form_helpers.html">Action View Form Helpers</option> </optgroup> <optgroup label="Controllers"> <option value="action_controller_overview.html">Action Controller Overview</option> <option value="action_controller_advanced_topics.html">Action Controller Advanced Topics</option> <option value="routing.html">Rails Routing from the Outside In</option> </optgroup> <optgroup label="Other Components"> <option value="active_support_core_extensions.html">Active Support Core Extensions</option> <option value="action_mailer_basics.html">Action Mailer Basics</option> <option value="action_mailbox_basics.html">Action Mailbox Basics</option> <option value="action_text_overview.html">Action Text Overview</option> <option value="active_job_basics.html">Active Job Basics</option> <option value="active_storage_overview.html">Active Storage Overview</option> <option value="action_cable_overview.html">Action Cable Overview</option> </optgroup> <optgroup label="Digging Deeper"> <option value="i18n.html">Rails Internationalization (I18n) API</option> <option value="testing.html">Testing Rails Applications</option> <option value="debugging_rails_applications.html">Debugging Rails Applications</option> <option value="configuring.html">Configuring Rails Applications</option> <option value="command_line.html">The Rails Command Line</option> <option value="asset_pipeline.html">The Asset Pipeline</option> <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option> <option value="autoloading_and_reloading_constants.html">Autoloading and Reloading</option> <option value="api_app.html">Using Rails for API-only Applications</option> </optgroup> <optgroup label="Going to Production"> <option value="tuning_performance_for_deployment.html">Tuning Performance for Deployment</option> <option value="caching_with_rails.html">Caching with Rails: An Overview</option> <option value="security.html">Securing Rails Applications</option> <option value="error_reporting.html">Error Reporting in Rails Applications</option> </optgroup> <optgroup label="Advanced Active Record"> <option value="active_record_multiple_databases.html">Multiple Databases</option> <option value="active_record_composite_primary_keys.html">Composite Primary Keys</option> </optgroup> <optgroup label="Extending Rails"> <option value="rails_on_rack.html">Rails on Rack</option> <option value="generators.html">Creating and Customizing Rails Generators & Templates</option> </optgroup> <optgroup label="Contributing"> <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option> <option value="api_documentation_guidelines.html">API Documentation Guidelines</option> <option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option> <option value="development_dependencies_install.html">Installing Rails Core Development Dependencies</option> </optgroup> <optgroup label="Policies"> <option value="maintenance_policy.html">Maintenance Policy</option> </optgroup> <optgroup label="Release Notes"> <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option> <option value="8_0_release_notes.html">Version 8.0 - November 2024</option> <option value="7_2_release_notes.html">Version 7.2 - August 2024</option> <option value="7_1_release_notes.html">Version 7.1 - October 2023</option> <option value="7_0_release_notes.html">Version 7.0 - December 2021</option> <option value="6_1_release_notes.html">Version 6.1 - December 2020</option> <option value="6_0_release_notes.html">Version 6.0 - August 2019</option> <option value="5_2_release_notes.html">Version 5.2 - April 2018</option> <option value="5_1_release_notes.html">Version 5.1 - April 2017</option> <option value="5_0_release_notes.html">Version 5.0 - June 2016</option> <option value="4_2_release_notes.html">Version 4.2 - December 2014</option> <option value="4_1_release_notes.html">Version 4.1 - April 2014</option> <option value="4_0_release_notes.html">Version 4.0 - June 2013</option> <option value="3_2_release_notes.html">Version 3.2 - January 2012</option> <option value="3_1_release_notes.html">Version 3.1 - August 2011</option> <option value="3_0_release_notes.html">Version 3.0 - August 2010</option> <option value="2_3_release_notes.html">Version 2.3 - March 2009</option> <option value="2_2_release_notes.html">Version 2.2 - November 2008</option> </optgroup> </select> </li> </ul> </nav> </div> </header> <hr class="hide" /> <main id="main"> <article> <header id="feature"> <div class="wrapper"> <h1>Action View Form Helpers</h1><p>Forms are a common interface for user input in web applications. However, form markup can be tedious to write and maintain because of the need to handle form controls, naming, and attributes. Rails simplifies this by providing view helpers, which are methods that output HTML form markup. This guide will help you understand the different helper methods and when to use each.</p><p>After reading this guide, you will know:</p> <ul> <li>How to create basic forms, such as a search form.</li> <li>How to work with model-based forms for creating and editing specific database records.</li> <li>How to generate select boxes from multiple types of data.</li> <li>What date and time helpers Rails provides.</li> <li>What makes a file upload form different.</li> <li>How to post forms to external resources and specify setting an <code>authenticity_token</code>.</li> <li>How to build complex forms.</li> </ul> <nav id="column-side" aria-label="Chapter" class="guide-index" data-turbo="false"> <a id="chapter-nav-skip-link" href="#article-body" class="skip-link"> Skip to article body </a> <h2 class="chapter"> <picture aria-hidden="true"> <!-- Using the `source` HTML tag to set the dark theme image --> <source srcset="images/icon_book-close-bookmark-1-wht.svg" media="(prefers-color-scheme: dark)" /> <img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" /> </picture> Chapters </h2> <ol class="chapters"> <li><a href="#working-with-basic-forms">Working with Basic Forms</a> <ul> <li><a href="#a-generic-search-form">A Generic Search Form</a></li> <li><a href="#helpers-for-generating-form-elements">Helpers for Generating Form Elements</a></li> <li><a href="#other-helpers-of-interest">Other Helpers of Interest</a></li> </ul></li> <li><a href="#creating-forms-with-model-objects">Creating Forms with Model Objects</a> <ul> <li><a href="#binding-a-form-to-an-object">Binding a Form to an Object</a></li> <li><a href="#relying-on-record-identification">Relying on Record Identification</a></li> <li><a href="#working-with-namespaces">Working with Namespaces</a></li> <li><a href="#forms-with-patch-put-or-delete-methods">Forms with PATCH, PUT, or DELETE Methods</a></li> </ul></li> <li><a href="#making-select-boxes-with-ease">Making Select Boxes with Ease</a> <ul> <li><a href="#option-groups-for-select-boxes">Option Groups for Select Boxes</a></li> <li><a href="#binding-select-boxes-to-model-objects">Binding Select Boxes to Model Objects</a></li> </ul></li> <li><a href="#using-date-and-time-form-helpers">Using Date and Time Form Helpers</a> <ul> <li><a href="#select-boxes-for-time-or-date-components">Select Boxes for Time or Date Components</a></li> <li><a href="#selecting-time-zone">Selecting Time Zone</a></li> </ul></li> <li><a href="#collection-related-helpers">Collection Related Helpers</a> <ul> <li><a href="#the-collection-select-helper">The <code>collection_select</code> Helper</a></li> <li><a href="#the-collection-radio-buttons-helper">The <code>collection_radio_buttons</code> Helper</a></li> <li><a href="#the-collection-checkboxes-helper">The <code>collection_checkboxes</code> Helper</a></li> </ul></li> <li><a href="#uploading-files">Uploading Files</a> <ul> <li><a href="#csv-file-upload-example">CSV File Upload Example</a></li> </ul></li> <li><a href="#customizing-form-builders">Customizing Form Builders</a></li> <li><a href="#form-input-naming-conventions-and-params-hash">Form Input Naming Conventions and <code>params</code> Hash</a> <ul> <li><a href="#basic-structure">Basic Structure</a></li> <li><a href="#combining-arrays-and-hashes">Combining Arrays and Hashes</a></li> <li><a href="#hashes-with-an-index">Hashes with an Index</a></li> </ul></li> <li><a href="#building-complex-forms">Building Complex Forms</a> <ul> <li><a href="#configuring-the-model-for-nested-attributes">Configuring the Model for Nested Attributes</a></li> <li><a href="#nested-forms-in-the-view">Nested Forms in the View</a></li> <li><a href="#permitting-parameters-in-the-controller">Permitting Parameters in the Controller</a></li> <li><a href="#removing-associated-objects">Removing Associated Objects</a></li> <li><a href="#preventing-empty-records">Preventing Empty Records</a></li> </ul></li> <li><a href="#forms-to-external-resources">Forms to External Resources</a></li> <li><a href="#using-tag-helpers-without-a-form-builder">Using Tag Helpers without a Form Builder</a></li> <li><a href="#using-form-tag-and-form-for">Using <code>form_tag</code> and <code>form_for</code></a></li> </ol> </nav> </div> </header> <div class="wrapper"> <div id="column-main"> <section id="article-body"> <p>This guide is not intended to be a complete list of all available form helpers. Please refer to <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers.html">the Rails API documentation</a> for an exhaustive list of form helpers and their arguments.</p><h2 id="working-with-basic-forms"><a class="anchorlink" href="#working-with-basic-forms" data-turbo="false"><span>1</span> Working with Basic Forms</a></h2><p>The main form helper is <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormHelper.html#method-i-form_with"><code>form_with</code></a>.</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> Form contents <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with do |form| %> Form contents <% end %> ">Copy</button> </div> <p>When called without arguments, it creates an HTML <code><form></code> tag with the value of the <code>method</code> attribute set to <code>post</code> and the value of the <code>action</code> attribute set to the current page. For example, assuming the current page is a home page at <code>/home</code>, the generated HTML will look like this:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/home"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">value=</span><span class="s">"Lz6ILqUEs2CGdDa-oz38TqcqQORavGnbGkG0CQA8zc8peOps-K7sHgFSTPSkBx89pQxh3p5zPIkjoOTiA_UWbQ"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">></span> Form contents <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/home" accept-charset="UTF-8" method="post"> <input type="hidden" name="authenticity_token" value="Lz6ILqUEs2CGdDa-oz38TqcqQORavGnbGkG0CQA8zc8peOps-K7sHgFSTPSkBx89pQxh3p5zPIkjoOTiA_UWbQ" autocomplete="off"> Form contents </form> ">Copy</button> </div> <p>Notice that the form contains an <code>input</code> element with type <code>hidden</code>. This <code>authenticity_token</code> hidden input is required for non-GET form submissions. This token is a security feature in Rails used to prevent cross-site request forgery (CSRF) attacks, and form helpers automatically generate it for every non-GET form (assuming the security feature is enabled). You can read more about it in the <a href="security.html#cross-site-request-forgery-csrf">Securing Rails Applications</a> guide.</p><h3 id="a-generic-search-form"><a class="anchorlink" href="#a-generic-search-form" data-turbo="false"><span>1.1</span> A Generic Search Form</a></h3><p>One of the most basic forms on the web is a search form. This form contains:</p> <ul> <li>a form element with "GET" method,</li> <li>a label for the input,</li> <li>a text input element, and</li> <li>a submit element.</li> </ul> <p>Here is how to create a search form with <code>form_with</code>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">url: </span><span class="s2">"/search"</span><span class="p">,</span> <span class="ss">method: :get</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:query</span><span class="p">,</span> <span class="s2">"Search for:"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">search_field</span> <span class="ss">:query</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">submit</span> <span class="s2">"Search"</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with url: "/search", method: :get do |form| %> <%= form.label :query, "Search for:" %> <%= form.search_field :query %> <%= form.submit "Search" %> <% end %> ">Copy</button> </div> <p>This will generate the following HTML:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/search"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"get"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"query"</span><span class="nt">></span>Search for:<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"search"</span> <span class="na">name=</span><span class="s">"query"</span> <span class="na">id=</span><span class="s">"query"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">name=</span><span class="s">"commit"</span> <span class="na">value=</span><span class="s">"Search"</span> <span class="na">data-disable-with=</span><span class="s">"Search"</span><span class="nt">></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/search" accept-charset="UTF-8" method="get"> <label for="query">Search for:</label> <input type="search" name="query" id="query"> <input type="submit" name="commit" value="Search" data-disable-with="Search"> </form> ">Copy</button> </div> <p>Notice that for the search form we are using the <code>url</code> option of <code>form_with</code>. Setting <code>url: "/search"</code> changes the form action value from the default current page path to <code>action="/search"</code>.</p><p>In general, passing <code>url: my_path</code> to <code>form_with</code> tells the form where to make the request. The other option is to pass Active Model objects to the form, as you will learn <a href="#creating-forms-with-model-objects">below</a>. You can also use <a href="routing.html#path-and-url-helpers">URL helpers</a>.</p><p>The search form example above also shows the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html">form builder</a> object. You will learn about the many helpers provided by the form builder object (like<code>form.label</code> and <code>form.text_field</code>) in the next section.</p><div class="interstitial info"><p>For every form <code>input</code> element, an <code>id</code> attribute is generated from its name (<code>"query"</code> in above example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.</p></div><div class="interstitial warning"><p>Use "GET" as the method for search forms. In general, Rails conventions encourage using the right HTTP verb for controller actions. Using "GET" for search allows users to bookmark a specific search.</p></div><h3 id="helpers-for-generating-form-elements"><a class="anchorlink" href="#helpers-for-generating-form-elements" data-turbo="false"><span>1.2</span> Helpers for Generating Form Elements</a></h3><p>The form builder object yielded by <code>form_with</code> provides many helper methods for generating common form elements such as text fields, checkboxes, and radio buttons.</p><p>The first argument to these methods is always the name of the input. This is useful to remember because when the form is submitted, that name will be passed to the controller along with the form data in the <code>params</code> hash. The name will be the key in the <code>params</code> for the value entered by the user for that field.</p><p>For example, if the form contains <code><%= form.text_field :query %></code>, then you would be able to get the value of this field in the controller with <code>params[:query]</code>.</p><p>When naming inputs, Rails uses certain conventions that make it possible to submit parameters with non-scalar values such as arrays or hashes, which will also be accessible in <code>params</code>. You can read more about them in the <a href="#form-input-naming-conventions-and-params-hash">Form Input Naming Conventions and Params Hash</a> section of this guide. For details on the precise usage of these helpers, please refer to the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormTagHelper.html">API documentation</a>.</p><h4 id="checkboxes"><a class="anchorlink" href="#checkboxes" data-turbo="false"><span>1.2.1</span> Checkboxes</a></h4><p>A Checkbox is a form control that allows for a single value to be selected or deselected. A group of Checkboxes is generally used to allow a user to choose one or more options from the group.</p><p>Here's an example with three checkboxes in a form:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">checkbox</span> <span class="ss">:biography</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:biography</span><span class="p">,</span> <span class="s2">"Biography"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">checkbox</span> <span class="ss">:romance</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:romance</span><span class="p">,</span> <span class="s2">"Romance"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">checkbox</span> <span class="ss">:mystery</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:mystery</span><span class="p">,</span> <span class="s2">"Mystery"</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.checkbox :biography %> <%= form.label :biography, "Biography" %> <%= form.checkbox :romance %> <%= form.label :romance, "Romance" %> <%= form.checkbox :mystery %> <%= form.label :mystery, "Mystery" %> ">Copy</button> </div> <p>The above will generate the following:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">name=</span><span class="s">"biography"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"0"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">name=</span><span class="s">"biography"</span> <span class="na">id=</span><span class="s">"biography"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"biography"</span><span class="nt">></span>Biography<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"romance"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"0"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">name=</span><span class="s">"romance"</span> <span class="na">id=</span><span class="s">"romance"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"romance"</span><span class="nt">></span>Romance<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"mystery"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"0"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">name=</span><span class="s">"mystery"</span> <span class="na">id=</span><span class="s">"mystery"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"mystery"</span><span class="nt">></span>Mystery<span class="nt"></label></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input name="biography" type="hidden" value="0" autocomplete="off"><input type="checkbox" value="1" name="biography" id="biography"> <label for="biography">Biography</label> <input name="romance" type="hidden" value="0" autocomplete="off"><input type="checkbox" value="1" name="romance" id="romance"> <label for="romance">Romance</label> <input name="mystery" type="hidden" value="0" autocomplete="off"><input type="checkbox" value="1" name="mystery" id="mystery"> <label for="mystery">Mystery</label> ">Copy</button> </div> <p>The first parameter to <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-checkbox"><code>checkbox</code></a> is the name of the input which can be found in the <code>params</code> hash. If the user has checked the "Biography" checkbox only, the <code>params</code> hash would contain:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"biography"</span> <span class="o">=></span> <span class="s2">"1"</span><span class="p">,</span> <span class="s2">"romance"</span> <span class="o">=></span> <span class="s2">"0"</span><span class="p">,</span> <span class="s2">"mystery"</span> <span class="o">=></span> <span class="s2">"0"</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "biography" => "1", "romance" => "0", "mystery" => "0" } ">Copy</button> </div> <p>You can use <code>params[:biography]</code> to check if that checkbox is selected by the user.</p><p>The checkbox's values (the values that will appear in <code>params</code>) can optionally be specified using the <code>checked_value</code> and <code>unchecked_value</code> parameters. See the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormHelper.html#method-i-checkbox">API documentation</a> for more details.</p><p>There is also a <code>collection_checkboxes</code>, which you can learn about in the <a href="#collection-related-helpers">Collection Related Helpers section</a>.</p><h4 id="radio-buttons"><a class="anchorlink" href="#radio-buttons" data-turbo="false"><span>1.2.2</span> Radio Buttons</a></h4><p>Radio buttons are form controls that only allow the user to select one option at a time from the list of choices.</p><p>For example, radio buttons for choosing your favorite ice cream flavor:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">radio_button</span> <span class="ss">:flavor</span><span class="p">,</span> <span class="s2">"chocolate_chip"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:flavor_chocolate_chip</span><span class="p">,</span> <span class="s2">"Chocolate Chip"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">radio_button</span> <span class="ss">:flavor</span><span class="p">,</span> <span class="s2">"vanilla"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:flavor_vanilla</span><span class="p">,</span> <span class="s2">"Vanilla"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">radio_button</span> <span class="ss">:flavor</span><span class="p">,</span> <span class="s2">"hazelnut"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:flavor_hazelnut</span><span class="p">,</span> <span class="s2">"Hazelnut"</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.radio_button :flavor, "chocolate_chip" %> <%= form.label :flavor_chocolate_chip, "Chocolate Chip" %> <%= form.radio_button :flavor, "vanilla" %> <%= form.label :flavor_vanilla, "Vanilla" %> <%= form.radio_button :flavor, "hazelnut" %> <%= form.label :flavor_hazelnut, "Hazelnut" %> ">Copy</button> </div> <p>The above will generate the following HTML:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">value=</span><span class="s">"chocolate_chip"</span> <span class="na">name=</span><span class="s">"flavor"</span> <span class="na">id=</span><span class="s">"flavor_chocolate_chip"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"flavor_chocolate_chip"</span><span class="nt">></span>Chocolate Chip<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">value=</span><span class="s">"vanilla"</span> <span class="na">name=</span><span class="s">"flavor"</span> <span class="na">id=</span><span class="s">"flavor_vanilla"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"flavor_vanilla"</span><span class="nt">></span>Vanilla<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">value=</span><span class="s">"hazelnut"</span> <span class="na">name=</span><span class="s">"flavor"</span> <span class="na">id=</span><span class="s">"flavor_hazelnut"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"flavor_hazelnut"</span><span class="nt">></span>Hazelnut<span class="nt"></label></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="radio" value="chocolate_chip" name="flavor" id="flavor_chocolate_chip"> <label for="flavor_chocolate_chip">Chocolate Chip</label> <input type="radio" value="vanilla" name="flavor" id="flavor_vanilla"> <label for="flavor_vanilla">Vanilla</label> <input type="radio" value="hazelnut" name="flavor" id="flavor_hazelnut"> <label for="flavor_hazelnut">Hazelnut</label> ">Copy</button> </div> <p>The second argument to <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-radio_button"><code>radio_button</code></a> is the value of the input. Because these radio buttons share the same name (<code>flavor</code>), the user will only be able to select one of them, and <code>params[:flavor]</code> will contain either <code>"chocolate_chip"</code>, <code>"vanilla"</code>, or <code>hazelnut</code>.</p><div class="interstitial note"><p>Always use labels for checkbox and radio buttons. They associate text with a specific option using the <code>for</code> attribute and, by expanding the clickable region, make it easier for users to click the inputs.</p></div><h3 id="other-helpers-of-interest"><a class="anchorlink" href="#other-helpers-of-interest" data-turbo="false"><span>1.3</span> Other Helpers of Interest</a></h3><p>There are many other form controls including text, email, password, date, and time. The below examples show some more helpers and their generated HTML.</p><p>Date and time related helpers:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">date_field</span> <span class="ss">:born_on</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">time_field</span> <span class="ss">:started_at</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">datetime_local_field</span> <span class="ss">:graduation_day</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">month_field</span> <span class="ss">:birthday_month</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">week_field</span> <span class="ss">:birthday_week</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.date_field :born_on %> <%= form.time_field :started_at %> <%= form.datetime_local_field :graduation_day %> <%= form.month_field :birthday_month %> <%= form.week_field :birthday_week %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"date"</span> <span class="na">name=</span><span class="s">"born_on"</span> <span class="na">id=</span><span class="s">"born_on"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"time"</span> <span class="na">name=</span><span class="s">"started_at"</span> <span class="na">id=</span><span class="s">"started_at"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"datetime-local"</span> <span class="na">name=</span><span class="s">"graduation_day"</span> <span class="na">id=</span><span class="s">"graduation_day"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"month"</span> <span class="na">name=</span><span class="s">"birthday_month"</span> <span class="na">id=</span><span class="s">"birthday_month"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"week"</span> <span class="na">name=</span><span class="s">"birthday_week"</span> <span class="na">id=</span><span class="s">"birthday_week"</span><span class="nt">></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="date" name="born_on" id="born_on"> <input type="time" name="started_at" id="started_at"> <input type="datetime-local" name="graduation_day" id="graduation_day"> <input type="month" name="birthday_month" id="birthday_month"> <input type="week" name="birthday_week" id="birthday_week"> ">Copy</button> </div> <p>Helpers with special formatting:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">password_field</span> <span class="ss">:password</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">email_field</span> <span class="ss">:address</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">telephone_field</span> <span class="ss">:phone</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">url_field</span> <span class="ss">:homepage</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.password_field :password %> <%= form.email_field :address %> <%= form.telephone_field :phone %> <%= form.url_field :homepage %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"password"</span> <span class="na">name=</span><span class="s">"password"</span> <span class="na">id=</span><span class="s">"password"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"email"</span> <span class="na">name=</span><span class="s">"address"</span> <span class="na">id=</span><span class="s">"address"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"tel"</span> <span class="na">name=</span><span class="s">"phone"</span> <span class="na">id=</span><span class="s">"phone"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"url"</span> <span class="na">name=</span><span class="s">"homepage"</span> <span class="na">id=</span><span class="s">"homepage"</span><span class="nt">></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="password" name="password" id="password"> <input type="email" name="address" id="address"> <input type="tel" name="phone" id="phone"> <input type="url" name="homepage" id="homepage"> ">Copy</button> </div> <p>Other common helpers:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">textarea</span> <span class="ss">:message</span><span class="p">,</span> <span class="ss">size: </span><span class="s2">"70x5"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">hidden_field</span> <span class="ss">:parent_id</span><span class="p">,</span> <span class="ss">value: </span><span class="s2">"foo"</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">number_field</span> <span class="ss">:price</span><span class="p">,</span> <span class="ss">in: </span><span class="mf">1.0</span><span class="o">..</span><span class="mf">20.0</span><span class="p">,</span> <span class="ss">step: </span><span class="mf">0.5</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">range_field</span> <span class="ss">:discount</span><span class="p">,</span> <span class="ss">in: </span><span class="mi">1</span><span class="o">..</span><span class="mi">100</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">search_field</span> <span class="ss">:name</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">color_field</span> <span class="ss">:favorite_color</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.textarea :message, size: "70x5" %> <%= form.hidden_field :parent_id, value: "foo" %> <%= form.number_field :price, in: 1.0..20.0, step: 0.5 %> <%= form.range_field :discount, in: 1..100 %> <%= form.search_field :name %> <%= form.color_field :favorite_color %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><textarea</span> <span class="na">name=</span><span class="s">"message"</span> <span class="na">id=</span><span class="s">"message"</span> <span class="na">cols=</span><span class="s">"70"</span> <span class="na">rows=</span><span class="s">"5"</span><span class="nt">></textarea></span> <span class="nt"><input</span> <span class="na">value=</span><span class="s">"foo"</span> <span class="na">autocomplete=</span><span class="s">"off"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"parent_id"</span> <span class="na">id=</span><span class="s">"parent_id"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">step=</span><span class="s">"0.5"</span> <span class="na">min=</span><span class="s">"1.0"</span> <span class="na">max=</span><span class="s">"20.0"</span> <span class="na">type=</span><span class="s">"number"</span> <span class="na">name=</span><span class="s">"price"</span> <span class="na">id=</span><span class="s">"price"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">min=</span><span class="s">"1"</span> <span class="na">max=</span><span class="s">"100"</span> <span class="na">type=</span><span class="s">"range"</span> <span class="na">name=</span><span class="s">"discount"</span> <span class="na">id=</span><span class="s">"discount"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"search"</span> <span class="na">name=</span><span class="s">"name"</span> <span class="na">id=</span><span class="s">"name"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">value=</span><span class="s">"#000000"</span> <span class="na">type=</span><span class="s">"color"</span> <span class="na">name=</span><span class="s">"favorite_color"</span> <span class="na">id=</span><span class="s">"favorite_color"</span><span class="nt">></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<textarea name="message" id="message" cols="70" rows="5"></textarea> <input value="foo" autocomplete="off" type="hidden" name="parent_id" id="parent_id"> <input step="0.5" min="1.0" max="20.0" type="number" name="price" id="price"> <input min="1" max="100" type="range" name="discount" id="discount"> <input type="search" name="name" id="name"> <input value="#000000" type="color" name="favorite_color" id="favorite_color"> ">Copy</button> </div> <p>Hidden inputs are not shown to the user but instead hold data like any textual input. Values inside them can be changed with JavaScript.</p><div class="interstitial info"><p>If you're using password input fields, you might want to configure your application to prevent those parameters from being logged. You can learn about how in the <a href="security.html#logging">Securing Rails Applications</a> guide.</p></div><h2 id="creating-forms-with-model-objects"><a class="anchorlink" href="#creating-forms-with-model-objects" data-turbo="false"><span>2</span> Creating Forms with Model Objects</a></h2><h3 id="binding-a-form-to-an-object"><a class="anchorlink" href="#binding-a-form-to-an-object" data-turbo="false"><span>2.1</span> Binding a Form to an Object</a></h3><p>The <code>form_with</code> helper has a <code>:model</code> option that allows you to bind the form builder object to a model object. This means that the form will be scoped to that model object, and the form's fields will be populated with values from that model object.</p><p>For example, if we have a <code>@book</code> model object:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="vi">@book</span> <span class="o">=</span> <span class="no">Book</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="c1"># => #<Book id: 42, title: "Walden", author: "Henry David Thoreau"></span> </code></pre> <button class="clipboard-button" data-clipboard-text="@book = Book.find(42) # => #<Book id: 42, title: "Walden", author: "Henry David Thoreau"> ">Copy</button> </div> <p>And the following form to create a new book:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@book</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="nt"><div></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:title</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="cp">%></span> <span class="nt"></div></span> <span class="nt"><div></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:author</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:author</span> <span class="cp">%></span> <span class="nt"></div></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">submit</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @book do |form| %> <div> <%= form.label :title %> <%= form.text_field :title %> </div> <div> <%= form.label :author %> <%= form.text_field :author %> </div> <%= form.submit %> <% end %> ">Copy</button> </div> <p>It will generate this HTML:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/books"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">value=</span><span class="s">"ChwHeyegcpAFDdBvXvDuvbfW7yCA3e8gvhyieai7DhG28C3akh-dyuv-IBittsjPrIjETlQQvQJ91T77QQ8xWA"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">></span> <span class="nt"><div></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"book_title"</span><span class="nt">></span>Title<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"book[title]"</span> <span class="na">id=</span><span class="s">"book_title"</span><span class="nt">></span> <span class="nt"></div></span> <span class="nt"><div></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"book_author"</span><span class="nt">></span>Author<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"book[author]"</span> <span class="na">id=</span><span class="s">"book_author"</span><span class="nt">></span> <span class="nt"></div></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">name=</span><span class="s">"commit"</span> <span class="na">value=</span><span class="s">"Create Book"</span> <span class="na">data-disable-with=</span><span class="s">"Create Book"</span><span class="nt">></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/books" accept-charset="UTF-8" method="post"> <input type="hidden" name="authenticity_token" value="ChwHeyegcpAFDdBvXvDuvbfW7yCA3e8gvhyieai7DhG28C3akh-dyuv-IBittsjPrIjETlQQvQJ91T77QQ8xWA" autocomplete="off"> <div> <label for="book_title">Title</label> <input type="text" name="book[title]" id="book_title"> </div> <div> <label for="book_author">Author</label> <input type="text" name="book[author]" id="book_author"> </div> <input type="submit" name="commit" value="Create Book" data-disable-with="Create Book"> </form> ">Copy</button> </div> <p>Some important things to notice when using <code>form_with</code> with a model object:</p> <ul> <li>The form <code>action</code> is automatically filled with an appropriate value, <code>action="/books"</code>. If you were updating a book, it would be <code>action="/books/42"</code>.</li> <li>The form field names are scoped with <code>book[...]</code>. This means that <code>params[:book]</code> will be a hash containing all these field's values. You can read more about the significance of input names in chapter <a href="#form-input-naming-conventions-and-params-hash">Form Input Naming Conventions and Params Hash</a> of this guide.</li> <li>The submit button is automatically given an appropriate text value, "Create Book" in this case.</li> </ul> <div class="interstitial info"><p>Typically your form inputs will mirror model attributes. However, they don't have to. If there is other information you need you can include a field in your form and access it via <code>params[:book][:my_non_attribute_input]</code>.</p></div><h4 id="composite-primary-key-forms"><a class="anchorlink" href="#composite-primary-key-forms" data-turbo="false"><span>2.1.1</span> Composite Primary Key Forms</a></h4><p>If you have a model with a <a href="active_record_composite_primary_keys.html">composite primary key</a>, the form building syntax is the same with slightly different output.</p><p>For example, to update a <code>@book</code> model object with a composite key <code>[:author_id, :id]</code> like this:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="vi">@book</span> <span class="o">=</span> <span class="no">Book</span><span class="p">.</span><span class="nf">find</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">25</span><span class="p">])</span> <span class="c1"># => #<Book id: 25, title: "Some book", author_id: 2></span> </code></pre> <button class="clipboard-button" data-clipboard-text="@book = Book.find([2, 25]) # => #<Book id: 25, title: "Some book", author_id: 2> ">Copy</button> </div> <p>The following form:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@book</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">submit</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @book do |form| %> <%= form.text_field :title %> <%= form.submit %> <% end %> ">Copy</button> </div> <p>Will generate this HTML output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/books/2_25"</span> <span class="na">method=</span><span class="s">"post"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="nt">></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"ChwHeyegcpAFDdBvXvDuvbfW7yCA3e8gvhyieai7DhG28C3akh-dyuv-IBittsjPrIjETlQQvQJ91T77QQ8xWA"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"book[title]"</span> <span class="na">id=</span><span class="s">"book_title"</span> <span class="na">value=</span><span class="s">"Some book"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">name=</span><span class="s">"commit"</span> <span class="na">value=</span><span class="s">"Update Book"</span> <span class="na">data-disable-with=</span><span class="s">"Update Book"</span><span class="nt">></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/books/2_25" method="post" accept-charset="UTF-8" > <input name="authenticity_token" type="hidden" value="ChwHeyegcpAFDdBvXvDuvbfW7yCA3e8gvhyieai7DhG28C3akh-dyuv-IBittsjPrIjETlQQvQJ91T77QQ8xWA" /> <input type="text" name="book[title]" id="book_title" value="Some book" /> <input type="submit" name="commit" value="Update Book" data-disable-with="Update Book"> </form> ">Copy</button> </div> <p>Note the generated URL contains the <code>author_id</code> and <code>id</code> delimited by an underscore. Once submitted, the controller can <a href="action_controller_overview.html#composite-key-parameters">extract each primary key value</a> from the parameters and update the record as it would with a singular primary key.</p><h4 id="the-fields-for-helper"><a class="anchorlink" href="#the-fields-for-helper" data-turbo="false"><span>2.1.2</span> The <code>fields_for</code> Helper</a></h4><p>The <code>fields_for</code> helper is used to render fields for related model objects within the same form. The associated "inner" model is usually related to the "main" form model via an Active Record association. For example, if you had a <code>Person</code> model with an associated <code>ContactDetail</code> model, you could create a single form with inputs for both models like so:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">person_form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">person_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:name</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">fields_for</span> <span class="ss">:contact_detail</span><span class="p">,</span> <span class="vi">@person</span><span class="p">.</span><span class="nf">contact_detail</span> <span class="k">do</span> <span class="o">|</span><span class="n">contact_detail_form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">contact_detail_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:phone_number</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |person_form| %> <%= person_form.text_field :name %> <%= fields_for :contact_detail, @person.contact_detail do |contact_detail_form| %> <%= contact_detail_form.text_field :phone_number %> <% end %> <% end %> ">Copy</button> </div> <p>The above will produce the following output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/people"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">value=</span><span class="s">"..."</span> <span class="na">autocomplete=</span><span class="s">"off"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"person[name]"</span> <span class="na">id=</span><span class="s">"person_name"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"contact_detail[phone_number]"</span> <span class="na">id=</span><span class="s">"contact_detail_phone_number"</span> <span class="nt">/></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/people" accept-charset="UTF-8" method="post"> <input type="hidden" name="authenticity_token" value="..." autocomplete="off" /> <input type="text" name="person[name]" id="person_name" /> <input type="text" name="contact_detail[phone_number]" id="contact_detail_phone_number" /> </form> ">Copy</button> </div> <p>The object yielded by <code>fields_for</code> is a form builder like the one yielded by <code>form_with</code>. The <code>fields_for</code> helper creates a similar binding but without rendering a <code><form></code> tag. You can learn more about <code>field_for</code> in the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for">API docs</a>.</p><h3 id="relying-on-record-identification"><a class="anchorlink" href="#relying-on-record-identification" data-turbo="false"><span>2.2</span> Relying on Record Identification</a></h3><p>When dealing with RESTful resources, calls to <code>form_with</code> can be simplified by relying on <strong>record identification</strong>. This means you pass the model instance and have Rails figure out the model name, method, and other things. In the example below for creating a new record, both calls to <code>form_with</code> generate the same HTML:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># longer way:</span> <span class="n">form_with</span><span class="p">(</span><span class="ss">model: </span><span class="vi">@article</span><span class="p">,</span> <span class="ss">url: </span><span class="n">articles_path</span><span class="p">)</span> <span class="c1"># short-hand:</span> <span class="n">form_with</span><span class="p">(</span><span class="ss">model: </span><span class="vi">@article</span><span class="p">)</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# longer way: form_with(model: @article, url: articles_path) # short-hand: form_with(model: @article) ">Copy</button> </div> <p>Similarly, for editing an existing article like below, both the calls to <code>form_with</code> will also generate the same HTML:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># longer way:</span> <span class="n">form_with</span><span class="p">(</span><span class="ss">model: </span><span class="vi">@article</span><span class="p">,</span> <span class="ss">url: </span><span class="n">article_path</span><span class="p">(</span><span class="vi">@article</span><span class="p">),</span> <span class="ss">method: </span><span class="s2">"patch"</span><span class="p">)</span> <span class="c1"># short-hand:</span> <span class="n">form_with</span><span class="p">(</span><span class="ss">model: </span><span class="vi">@article</span><span class="p">)</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# longer way: form_with(model: @article, url: article_path(@article), method: "patch") # short-hand: form_with(model: @article) ">Copy</button> </div> <p>Notice how the short-hand <code>form_with</code> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <a href="https://api.rubyonrails.org/v8.0.1/classes/ActiveRecord/Persistence.html#method-i-persisted-3F"><code>record.persisted?</code></a>. It also selects the correct path to submit to, and the name based on the class of the object.</p><p>This is assuming that the <code>Article</code> model is declared with <code>resources :articles</code> in the routes file.</p><p>If you have a <a href="routing.html#singular-resources">singular resource</a>, you will need to call <code>resource</code> and <code>resolve</code> for it to work with <code>form_with</code>:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">resource</span> <span class="ss">:article</span> <span class="n">resolve</span><span class="p">(</span><span class="s2">"Article"</span><span class="p">)</span> <span class="p">{</span> <span class="p">[</span><span class="ss">:article</span><span class="p">]</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="resource :article resolve("Article") { [:article] } ">Copy</button> </div> <div class="interstitial info"><p>Declaring a resource has a number of side effects. See the <a href="routing.html#resource-routing-the-rails-default">Rails Routing from the Outside In</a> guide for more information on setting up and using resources.</p></div><div class="interstitial warning"><p>When you're using <a href="association_basics.html#single-table-inheritance-sti">single-table inheritance</a> with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify <code>:url</code>, and <code>:scope</code> (the model name) explicitly.</p></div><h3 id="working-with-namespaces"><a class="anchorlink" href="#working-with-namespaces" data-turbo="false"><span>2.3</span> Working with Namespaces</a></h3><p>If you have namespaced routes, <code>form_with</code> has a shorthand for that. For example, if your application has an <code>admin</code> namespace:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">form_with</span> <span class="ss">model: </span><span class="p">[</span><span class="ss">:admin</span><span class="p">,</span> <span class="vi">@article</span><span class="p">]</span> </code></pre> <button class="clipboard-button" data-clipboard-text="form_with model: [:admin, @article] ">Copy</button> </div> <p>The above will create a form that submits to the <code>Admin::ArticlesController</code> inside the admin namespace, therefore submitting to <code>admin_article_path(@article)</code> in the case of an update.</p><p>If you have several levels of namespacing then the syntax is similar:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">form_with</span> <span class="ss">model: </span><span class="p">[</span><span class="ss">:admin</span><span class="p">,</span> <span class="ss">:management</span><span class="p">,</span> <span class="vi">@article</span><span class="p">]</span> </code></pre> <button class="clipboard-button" data-clipboard-text="form_with model: [:admin, :management, @article] ">Copy</button> </div> <p>For more information on Rails' routing system and the associated conventions, please see the <a href="routing.html">Rails Routing from the Outside In</a> guide.</p><h3 id="forms-with-patch-put-or-delete-methods"><a class="anchorlink" href="#forms-with-patch-put-or-delete-methods" data-turbo="false"><span>2.4</span> Forms with PATCH, PUT, or DELETE Methods</a></h3><p>The Rails framework encourages RESTful design, which means forms in your application will make requests where the <code>method</code> is <code>PATCH</code>, <code>PUT</code>, or <code>DELETE</code> in addition to <code>GET</code> and <code>POST</code>. However, HTML forms <em>don't support</em> methods other than <code>GET</code> and <code>POST</code> when it comes to submitting forms.</p><p>Rails works around this limitation by emulating other methods over POST with a hidden input named <code>"_method"</code>. For example:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">form_with</span><span class="p">(</span><span class="ss">url: </span><span class="n">search_path</span><span class="p">,</span> <span class="ss">method: </span><span class="s2">"patch"</span><span class="p">)</span> </code></pre> <button class="clipboard-button" data-clipboard-text="form_with(url: search_path, method: "patch") ">Copy</button> </div> <p>The above form Will generate this HTML output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/search"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"_method"</span> <span class="na">value=</span><span class="s">"patch"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">value=</span><span class="s">"R4quRuXQAq75TyWpSf8AwRyLt-R1uMtPP1dHTTWJE5zbukiaY8poSTXxq3Z7uAjXfPHiKQDsWE1i2_-h0HSktQ"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">></span> <span class="c"><!-- ... --></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/search" accept-charset="UTF-8" method="post"> <input type="hidden" name="_method" value="patch" autocomplete="off"> <input type="hidden" name="authenticity_token" value="R4quRuXQAq75TyWpSf8AwRyLt-R1uMtPP1dHTTWJE5zbukiaY8poSTXxq3Z7uAjXfPHiKQDsWE1i2_-h0HSktQ" autocomplete="off"> <!-- ... --> </form> ">Copy</button> </div> <p>When parsing POSTed data, Rails will take into account the special <code>_method</code> parameter and proceed as if the request's HTTP method was the one set as the value of <code>_method</code> (<code>PATCH</code> in this example).</p><p>When rendering a form, submission buttons can override the declared <code>method</code> attribute through the <code>formmethod:</code> keyword:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">url: </span><span class="s2">"/posts/1"</span><span class="p">,</span> <span class="ss">method: :patch</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">button</span> <span class="s2">"Delete"</span><span class="p">,</span> <span class="ss">formmethod: :delete</span><span class="p">,</span> <span class="ss">data: </span><span class="p">{</span> <span class="ss">confirm: </span><span class="s2">"Are you sure?"</span> <span class="p">}</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">button</span> <span class="s2">"Update"</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with url: "/posts/1", method: :patch do |form| %> <%= form.button "Delete", formmethod: :delete, data: { confirm: "Are you sure?" } %> <%= form.button "Update" %> <% end %> ">Copy</button> </div> <p>Similar to <code><form></code> elements, most browsers <em>don't support</em> overriding form methods declared through <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod">formmethod</a> other than <code>GET</code> and <code>POST</code>.</p><p>Rails works around this issue by emulating other methods over POST through a combination of <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod">formmethod</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-value">value</a>, and <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-name">name</a> attributes:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">action=</span><span class="s">"/posts/1"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"_method"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"patch"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"f755bb0ed134b76c432144748a6d4b7a7ddf2b71"</span> <span class="nt">/></span> <span class="c"><!-- ... --></span> <span class="nt"><button</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">formmethod=</span><span class="s">"post"</span> <span class="na">name=</span><span class="s">"_method"</span> <span class="na">value=</span><span class="s">"delete"</span> <span class="na">data-confirm=</span><span class="s">"Are you sure?"</span><span class="nt">></span>Delete<span class="nt"></button></span> <span class="nt"><button</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">name=</span><span class="s">"button"</span><span class="nt">></span>Update<span class="nt"></button></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form accept-charset="UTF-8" action="/posts/1" method="post"> <input name="_method" type="hidden" value="patch" /> <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" /> <!-- ... --> <button type="submit" formmethod="post" name="_method" value="delete" data-confirm="Are you sure?">Delete</button> <button type="submit" name="button">Update</button> </form> ">Copy</button> </div> <p>In this case, the "Update" button will be treated as <code>PATCH</code> and the "Delete" button will be treated as <code>DELETE</code>.</p><h2 id="making-select-boxes-with-ease"><a class="anchorlink" href="#making-select-boxes-with-ease" data-turbo="false"><span>3</span> Making Select Boxes with Ease</a></h2><p>Select boxes, also known as drop-down list, allow users to select from a list of options. The HTML for select boxes requires a decent amount of markup - one <code><option></code> element for each option to choose from. Rails provides helper methods to help generate that markup.</p><p>For example, let's say we have a list of cities for the user to choose from. We can use the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-select"><code>select</code></a> helper:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">select</span> <span class="ss">:city</span><span class="p">,</span> <span class="p">[</span><span class="s2">"Berlin"</span><span class="p">,</span> <span class="s2">"Chicago"</span><span class="p">,</span> <span class="s2">"Madrid"</span><span class="p">]</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.select :city, ["Berlin", "Chicago", "Madrid"] %> ">Copy</button> </div> <p>The above will generate this HTML output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"city"</span> <span class="na">id=</span><span class="s">"city"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Berlin"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Chicago"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Madrid"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="city" id="city"> <option value="Berlin">Berlin</option> <option value="Chicago">Chicago</option> <option value="Madrid">Madrid</option> </select> ">Copy</button> </div> <p>And the selection will be available in <code>params[:city]</code> as usual.</p><p>We can also specify <code><option></code> values that differ from their labels:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">select</span> <span class="ss">:city</span><span class="p">,</span> <span class="p">[[</span><span class="s2">"Berlin"</span><span class="p">,</span> <span class="s2">"BE"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Chicago"</span><span class="p">,</span> <span class="s2">"CHI"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Madrid"</span><span class="p">,</span> <span class="s2">"MD"</span><span class="p">]]</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.select :city, [["Berlin", "BE"], ["Chicago", "CHI"], ["Madrid", "MD"]] %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"city"</span> <span class="na">id=</span><span class="s">"city"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"BE"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"CHI"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"MD"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="city" id="city"> <option value="BE">Berlin</option> <option value="CHI">Chicago</option> <option value="MD">Madrid</option> </select> ">Copy</button> </div> <p>This way, the user will see the full city name, but <code>params[:city]</code> will be one of <code>"BE"</code>, <code>"CHI"</code>, or <code>"MD"</code>.</p><p>Lastly, we can specify a default choice for the select box with the <code>:selected</code> argument:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">select</span> <span class="ss">:city</span><span class="p">,</span> <span class="p">[[</span><span class="s2">"Berlin"</span><span class="p">,</span> <span class="s2">"BE"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Chicago"</span><span class="p">,</span> <span class="s2">"CHI"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Madrid"</span><span class="p">,</span> <span class="s2">"MD"</span><span class="p">]],</span> <span class="ss">selected: </span><span class="s2">"CHI"</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.select :city, [["Berlin", "BE"], ["Chicago", "CHI"], ["Madrid", "MD"]], selected: "CHI" %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"city"</span> <span class="na">id=</span><span class="s">"city"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"BE"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"CHI"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"MD"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="city" id="city"> <option value="BE">Berlin</option> <option value="CHI" selected="selected">Chicago</option> <option value="MD">Madrid</option> </select> ">Copy</button> </div> <h3 id="option-groups-for-select-boxes"><a class="anchorlink" href="#option-groups-for-select-boxes" data-turbo="false"><span>3.1</span> Option Groups for Select Boxes</a></h3><p>In some cases we may want to improve the user experience by grouping related options together. We can do so by passing a <code>Hash</code> (or comparable <code>Array</code>) to <code>select</code>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">select</span> <span class="ss">:city</span><span class="p">,</span> <span class="p">{</span> <span class="s2">"Europe"</span> <span class="o">=></span> <span class="p">[</span> <span class="p">[</span><span class="s2">"Berlin"</span><span class="p">,</span> <span class="s2">"BE"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Madrid"</span><span class="p">,</span> <span class="s2">"MD"</span><span class="p">]</span> <span class="p">],</span> <span class="s2">"North America"</span> <span class="o">=></span> <span class="p">[</span> <span class="p">[</span><span class="s2">"Chicago"</span><span class="p">,</span> <span class="s2">"CHI"</span><span class="p">]</span> <span class="p">],</span> <span class="p">},</span> <span class="ss">selected: </span><span class="s2">"CHI"</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.select :city, { "Europe" => [ ["Berlin", "BE"], ["Madrid", "MD"] ], "North America" => [ ["Chicago", "CHI"] ], }, selected: "CHI" %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"city"</span> <span class="na">id=</span><span class="s">"city"</span><span class="nt">></span> <span class="nt"><optgroup</span> <span class="na">label=</span><span class="s">"Europe"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"BE"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"MD"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></optgroup></span> <span class="nt"><optgroup</span> <span class="na">label=</span><span class="s">"North America"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"CHI"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"></optgroup></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="city" id="city"> <optgroup label="Europe"> <option value="BE">Berlin</option> <option value="MD">Madrid</option> </optgroup> <optgroup label="North America"> <option value="CHI" selected="selected">Chicago</option> </optgroup> </select> ">Copy</button> </div> <h3 id="binding-select-boxes-to-model-objects"><a class="anchorlink" href="#binding-select-boxes-to-model-objects" data-turbo="false"><span>3.2</span> Binding Select Boxes to Model Objects</a></h3><p>Like other form controls, a select box can be bound to a model attribute. For example, if we have a <code>@person</code> model object like:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="vi">@person</span> <span class="o">=</span> <span class="no">Person</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">city: </span><span class="s2">"MD"</span><span class="p">)</span> </code></pre> <button class="clipboard-button" data-clipboard-text="@person = Person.new(city: "MD") ">Copy</button> </div> <p>The following form:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">select</span> <span class="ss">:city</span><span class="p">,</span> <span class="p">[[</span><span class="s2">"Berlin"</span><span class="p">,</span> <span class="s2">"BE"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Chicago"</span><span class="p">,</span> <span class="s2">"CHI"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"Madrid"</span><span class="p">,</span> <span class="s2">"MD"</span><span class="p">]]</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> <%= form.select :city, [["Berlin", "BE"], ["Chicago", "CHI"], ["Madrid", "MD"]] %> <% end %> ">Copy</button> </div> <p>Will output this select box:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"person[city]"</span> <span class="na">id=</span><span class="s">"person_city"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"BE"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"CHI"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"MD"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="person[city]" id="person_city"> <option value="BE">Berlin</option> <option value="CHI">Chicago</option> <option value="MD" selected="selected">Madrid</option> </select> ">Copy</button> </div> <p>The only difference is that the selected option will be found in <code>params[:person][:city]</code> instead of <code>params[:city]</code>.</p><p>Notice that the appropriate option was automatically marked <code>selected="selected"</code>. Since this select box was bound to an existing <code>@person</code> record, we didn't need to specify a <code>:selected</code> argument.</p><h2 id="using-date-and-time-form-helpers"><a class="anchorlink" href="#using-date-and-time-form-helpers" data-turbo="false"><span>4</span> Using Date and Time Form Helpers</a></h2><p>In addition to the <code>date_field</code> and <code>time_field</code> helpers mentioned <a href="#other-helpers-of-interest">earlier</a>, Rails provides alternative date and time form helpers that render plain select boxes. The <code>date_select</code> helper renders a separate select box for year, month, and day.</p><p>For example, if we have a <code>@person</code> model object like:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="vi">@person</span> <span class="o">=</span> <span class="no">Person</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">birth_date: </span><span class="no">Date</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="mi">1995</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">21</span><span class="p">))</span> </code></pre> <button class="clipboard-button" data-clipboard-text="@person = Person.new(birth_date: Date.new(1995, 12, 21)) ">Copy</button> </div> <p>The following form:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">date_select</span> <span class="ss">:birth_date</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> <%= form.date_select :birth_date %> <% end %> ">Copy</button> </div> <p>Will output select boxes like:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"person[birth_date(1i)]"</span> <span class="na">id=</span><span class="s">"person_birth_date_1i"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1990"</span><span class="nt">></span>1990<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1991"</span><span class="nt">></span>1991<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1992"</span><span class="nt">></span>1992<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1993"</span><span class="nt">></span>1993<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1994"</span><span class="nt">></span>1994<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1995"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>1995<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1996"</span><span class="nt">></span>1996<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1997"</span><span class="nt">></span>1997<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1998"</span><span class="nt">></span>1998<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1999"</span><span class="nt">></span>1999<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2000"</span><span class="nt">></span>2000<span class="nt"></option></span> <span class="nt"></select></span> <span class="nt"><select</span> <span class="na">name=</span><span class="s">"person[birth_date(2i)]"</span> <span class="na">id=</span><span class="s">"person_birth_date_2i"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1"</span><span class="nt">></span>January<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2"</span><span class="nt">></span>February<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"3"</span><span class="nt">></span>March<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"4"</span><span class="nt">></span>April<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"5"</span><span class="nt">></span>May<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"6"</span><span class="nt">></span>June<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"7"</span><span class="nt">></span>July<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"8"</span><span class="nt">></span>August<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"9"</span><span class="nt">></span>September<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"10"</span><span class="nt">></span>October<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"11"</span><span class="nt">></span>November<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"12"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>December<span class="nt"></option></span> <span class="nt"></select></span> <span class="nt"><select</span> <span class="na">name=</span><span class="s">"person[birth_date(3i)]"</span> <span class="na">id=</span><span class="s">"person_birth_date_3i"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1"</span><span class="nt">></span>1<span class="nt"></option></span> ... <span class="nt"><option</span> <span class="na">value=</span><span class="s">"21"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>21<span class="nt"></option></span> ... <span class="nt"><option</span> <span class="na">value=</span><span class="s">"31"</span><span class="nt">></span>31<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="person[birth_date(1i)]" id="person_birth_date_1i"> <option value="1990">1990</option> <option value="1991">1991</option> <option value="1992">1992</option> <option value="1993">1993</option> <option value="1994">1994</option> <option value="1995" selected="selected">1995</option> <option value="1996">1996</option> <option value="1997">1997</option> <option value="1998">1998</option> <option value="1999">1999</option> <option value="2000">2000</option> </select> <select name="person[birth_date(2i)]" id="person_birth_date_2i"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12" selected="selected">December</option> </select> <select name="person[birth_date(3i)]" id="person_birth_date_3i"> <option value="1">1</option> ... <option value="21" selected="selected">21</option> ... <option value="31">31</option> </select> ">Copy</button> </div> <p>Notice that, when the form is submitted, there will be no single value in the <code>params</code> hash that contains the full date. Instead, there will be several values with special names like <code>"birth_date(1i)"</code>. However, Active Model knows how to assemble these values into a full date, based on the declared type of the model attribute. So we can pass <code>params[:person]</code> to <code>Person.new</code> or <code>Person#update</code> just like we would if the form used a single field to represent the full date.</p><p>In addition to the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-date_select"><code>date_select</code></a> helper, Rails provides <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-time_select"><code>time_select</code></a> which outputs select boxes for the hour and minute. There is <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-datetime_select"><code>datetime_select</code></a> as well which combines both date and time select boxes.</p><h3 id="select-boxes-for-time-or-date-components"><a class="anchorlink" href="#select-boxes-for-time-or-date-components" data-turbo="false"><span>4.1</span> Select Boxes for Time or Date Components</a></h3><p>Rails also provides helpers to render select boxes for individual date and time components: <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/DateHelper.html#method-i-select_year"><code>select_year</code></a>, <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/DateHelper.html#method-i-select_month"><code>select_month</code></a>, <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/DateHelper.html#method-i-select_day"><code>select_day</code></a>, <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/DateHelper.html#method-i-select_hour"><code>select_hour</code></a>, <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/DateHelper.html#method-i-select_minute"><code>select_minute</code></a>, and <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/DateHelper.html#method-i-select_second"><code>select_second</code></a>. These helpers are "bare" methods, meaning they are not called on a form builder instance. For example:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">select_year</span> <span class="mi">2024</span><span class="p">,</span> <span class="ss">prefix: </span><span class="s2">"party"</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= select_year 2024, prefix: "party" %> ">Copy</button> </div> <p>The above outputs a select box like:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">id=</span><span class="s">"party_year"</span> <span class="na">name=</span><span class="s">"party[year]"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2019"</span><span class="nt">></span>2019<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2020"</span><span class="nt">></span>2020<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2021"</span><span class="nt">></span>2021<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2022"</span><span class="nt">></span>2022<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2023"</span><span class="nt">></span>2023<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2024"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>2024<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2025"</span><span class="nt">></span>2025<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2026"</span><span class="nt">></span>2026<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2027"</span><span class="nt">></span>2027<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2028"</span><span class="nt">></span>2028<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2029"</span><span class="nt">></span>2029<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select id="party_year" name="party[year]"> <option value="2019">2019</option> <option value="2020">2020</option> <option value="2021">2021</option> <option value="2022">2022</option> <option value="2023">2023</option> <option value="2024" selected="selected">2024</option> <option value="2025">2025</option> <option value="2026">2026</option> <option value="2027">2027</option> <option value="2028">2028</option> <option value="2029">2029</option> </select> ">Copy</button> </div> <p>For each of these helpers, you may specify a <code>Date</code> or <code>Time</code> object instead of a number as the default value (for example <code><%= select_year Date.today, prefix: "party" %></code> instead of the above), and the appropriate date and time parts will be extracted and used.</p><h3 id="selecting-time-zone"><a class="anchorlink" href="#selecting-time-zone" data-turbo="false"><span>4.2</span> Selecting Time Zone</a></h3><p>When you need to ask users what time zone they are in, there is a very convenient <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-time_zone_select"><code>time_zone_select</code></a> helper to use.</p><p>Typically, you would have to provide a list of time zone options for users to select from. This can get tedious if not for the list of pre-defined <a href="https://api.rubyonrails.org/v8.0.1/classes/ActiveSupport/TimeZone.html"><code>ActiveSupport::TimeZone</code></a> objects. The <code>time_with_zone</code> helper wraps this and can be used as follows:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">time_zone_select</span> <span class="ss">:time_zone</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.time_zone_select :time_zone %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"time_zone"</span> <span class="na">id=</span><span class="s">"time_zone"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"International Date Line West"</span><span class="nt">></span>(GMT-12:00) International Date Line West<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"American Samoa"</span><span class="nt">></span>(GMT-11:00) American Samoa<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Midway Island"</span><span class="nt">></span>(GMT-11:00) Midway Island<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Hawaii"</span><span class="nt">></span>(GMT-10:00) Hawaii<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Alaska"</span><span class="nt">></span>(GMT-09:00) Alaska<span class="nt"></option></span> ... <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Samoa"</span><span class="nt">></span>(GMT+13:00) Samoa<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"Tokelau Is."</span><span class="nt">></span>(GMT+13:00) Tokelau Is.<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="time_zone" id="time_zone"> <option value="International Date Line West">(GMT-12:00) International Date Line West</option> <option value="American Samoa">(GMT-11:00) American Samoa</option> <option value="Midway Island">(GMT-11:00) Midway Island</option> <option value="Hawaii">(GMT-10:00) Hawaii</option> <option value="Alaska">(GMT-09:00) Alaska</option> ... <option value="Samoa">(GMT+13:00) Samoa</option> <option value="Tokelau Is.">(GMT+13:00) Tokelau Is.</option> </select> ">Copy</button> </div> <h2 id="collection-related-helpers"><a class="anchorlink" href="#collection-related-helpers" data-turbo="false"><span>5</span> Collection Related Helpers</a></h2><p>If you need to generate a set of choices from a collection of arbitrary objects, Rails has <code>collection_select</code>, <code>collection_radio_button</code>, and <code>collection_checkboxes</code> helpers.</p><p>To see when these helpers are useful, suppose you have a <code>City</code> model and corresponding <code>belongs_to :city</code> association with <code>Person</code>:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">City</span> <span class="o"><</span> <span class="no">ApplicationRecord</span> <span class="k">end</span> <span class="k">class</span> <span class="nc">Person</span> <span class="o"><</span> <span class="no">ApplicationRecord</span> <span class="n">belongs_to</span> <span class="ss">:city</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class City < ApplicationRecord end class Person < ApplicationRecord belongs_to :city end ">Copy</button> </div> <p>Assuming we have the following cities stored in the database:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="no">City</span><span class="p">.</span><span class="nf">order</span><span class="p">(</span><span class="ss">:name</span><span class="p">).</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">city</span><span class="o">|</span> <span class="p">[</span><span class="n">city</span><span class="p">.</span><span class="nf">name</span><span class="p">,</span> <span class="n">city</span><span class="p">.</span><span class="nf">id</span><span class="p">]</span> <span class="p">}</span> <span class="c1"># => [["Berlin", 1], ["Chicago", 3], ["Madrid", 2]]</span> </code></pre> <button class="clipboard-button" data-clipboard-text="City.order(:name).map { |city| [city.name, city.id] } # => [["Berlin", 1], ["Chicago", 3], ["Madrid", 2]] ">Copy</button> </div> <p>We can allow the user to choose from the cities with the following form:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">select</span> <span class="ss">:city_id</span><span class="p">,</span> <span class="no">City</span><span class="p">.</span><span class="nf">order</span><span class="p">(</span><span class="ss">:name</span><span class="p">).</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">city</span><span class="o">|</span> <span class="p">[</span><span class="n">city</span><span class="p">.</span><span class="nf">name</span><span class="p">,</span> <span class="n">city</span><span class="p">.</span><span class="nf">id</span><span class="p">]</span> <span class="p">}</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> <%= form.select :city_id, City.order(:name).map { |city| [city.name, city.id] } %> <% end %> ">Copy</button> </div> <p>The above will generate this HTML:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"person[city_id]"</span> <span class="na">id=</span><span class="s">"person_city_id"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"3"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="person[city_id]" id="person_city_id"> <option value="1">Berlin</option> <option value="3">Chicago</option> <option value="2">Madrid</option> </select> ">Copy</button> </div> <p>The above example shows how you'd generate the choices manually. However, Rails has helpers that generate choices from a collection without having to explicitly iterate over it. These helpers determine the value and text label of each choice by calling specified methods on each object in the collection.</p><div class="interstitial note"><p>When rendering a field for a <code>belongs_to</code> association, you must specify the name of the foreign key (<code>city_id</code> in the above example), rather than the name of the association itself.</p></div><h3 id="the-collection-select-helper"><a class="anchorlink" href="#the-collection-select-helper" data-turbo="false"><span>5.1</span> The <code>collection_select</code> Helper</a></h3><p>To generate a select box, we can use <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_select"><code>collection_select</code></a>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">collection_select</span> <span class="ss">:city_id</span><span class="p">,</span> <span class="no">City</span><span class="p">.</span><span class="nf">order</span><span class="p">(</span><span class="ss">:name</span><span class="p">),</span> <span class="ss">:id</span><span class="p">,</span> <span class="ss">:name</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.collection_select :city_id, City.order(:name), :id, :name %> ">Copy</button> </div> <p>The above outputs the same HTML as the manual iteration above:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><select</span> <span class="na">name=</span><span class="s">"person[city_id]"</span> <span class="na">id=</span><span class="s">"person_city_id"</span><span class="nt">></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"1"</span><span class="nt">></span>Berlin<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"3"</span><span class="nt">></span>Chicago<span class="nt"></option></span> <span class="nt"><option</span> <span class="na">value=</span><span class="s">"2"</span><span class="nt">></span>Madrid<span class="nt"></option></span> <span class="nt"></select></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<select name="person[city_id]" id="person_city_id"> <option value="1">Berlin</option> <option value="3">Chicago</option> <option value="2">Madrid</option> </select> ">Copy</button> </div> <div class="interstitial note"><p>The order of arguments for <code>collection_select</code> is different from the order for <code>select</code>. With <code>collection_select</code> we specify the value method first (<code>:id</code> in the example above), and the text label method second (<code>:name</code> in the example above). This is opposite of the order used when specifying choices for the <code>select</code> helper, where the text label comes first and the value second (<code>["Berlin", 1]</code> in the previous example).</p></div><h3 id="the-collection-radio-buttons-helper"><a class="anchorlink" href="#the-collection-radio-buttons-helper" data-turbo="false"><span>5.2</span> The <code>collection_radio_buttons</code> Helper</a></h3><p>To generate a set of radio buttons, we can use <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_radio_buttons"><code>collection_radio_buttons</code></a>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">collection_radio_buttons</span> <span class="ss">:city_id</span><span class="p">,</span> <span class="no">City</span><span class="p">.</span><span class="nf">order</span><span class="p">(</span><span class="ss">:name</span><span class="p">),</span> <span class="ss">:id</span><span class="p">,</span> <span class="ss">:name</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.collection_radio_buttons :city_id, City.order(:name), :id, :name %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">name=</span><span class="s">"person[city_id]"</span> <span class="na">id=</span><span class="s">"person_city_id_1"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_city_id_1"</span><span class="nt">></span>Berlin<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">value=</span><span class="s">"3"</span> <span class="na">name=</span><span class="s">"person[city_id]"</span> <span class="na">id=</span><span class="s">"person_city_id_3"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_city_id_3"</span><span class="nt">></span>Chicago<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">value=</span><span class="s">"2"</span> <span class="na">name=</span><span class="s">"person[city_id]"</span> <span class="na">id=</span><span class="s">"person_city_id_2"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_city_id_2"</span><span class="nt">></span>Madrid<span class="nt"></label></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="radio" value="1" name="person[city_id]" id="person_city_id_1"> <label for="person_city_id_1">Berlin</label> <input type="radio" value="3" name="person[city_id]" id="person_city_id_3"> <label for="person_city_id_3">Chicago</label> <input type="radio" value="2" name="person[city_id]" id="person_city_id_2"> <label for="person_city_id_2">Madrid</label> ">Copy</button> </div> <h3 id="the-collection-checkboxes-helper"><a class="anchorlink" href="#the-collection-checkboxes-helper" data-turbo="false"><span>5.3</span> The <code>collection_checkboxes</code> Helper</a></h3><p>To generate a set of check boxes — for example, to support a <code>has_and_belongs_to_many</code> association — we can use <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_checkboxes"><code>collection_checkboxes</code></a>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">collection_checkboxes</span> <span class="ss">:interest_ids</span><span class="p">,</span> <span class="no">Interest</span><span class="p">.</span><span class="nf">order</span><span class="p">(</span><span class="ss">:name</span><span class="p">),</span> <span class="ss">:id</span><span class="p">,</span> <span class="ss">:name</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form.collection_checkboxes :interest_ids, Interest.order(:name), :id, :name %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"person[interest_id][]"</span> <span class="na">value=</span><span class="s">"3"</span> <span class="na">id=</span><span class="s">"person_interest_id_3"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_interest_id_3"</span><span class="nt">></span>Engineering<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"person[interest_id][]"</span> <span class="na">value=</span><span class="s">"4"</span> <span class="na">id=</span><span class="s">"person_interest_id_4"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_interest_id_4"</span><span class="nt">></span>Math<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"person[interest_id][]"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">id=</span><span class="s">"person_interest_id_1"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_interest_id_1"</span><span class="nt">></span>Science<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"person[interest_id][]"</span> <span class="na">value=</span><span class="s">"2"</span> <span class="na">id=</span><span class="s">"person_interest_id_2"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_interest_id_2"</span><span class="nt">></span>Technology<span class="nt"></label></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="checkbox" name="person[interest_id][]" value="3" id="person_interest_id_3"> <label for="person_interest_id_3">Engineering</label> <input type="checkbox" name="person[interest_id][]" value="4" id="person_interest_id_4"> <label for="person_interest_id_4">Math</label> <input type="checkbox" name="person[interest_id][]" value="1" id="person_interest_id_1"> <label for="person_interest_id_1">Science</label> <input type="checkbox" name="person[interest_id][]" value="2" id="person_interest_id_2"> <label for="person_interest_id_2">Technology</label> ">Copy</button> </div> <h2 id="uploading-files"><a class="anchorlink" href="#uploading-files" data-turbo="false"><span>6</span> Uploading Files</a></h2><p>A common task with forms is allowing users to upload a file. It could be an avatar image or a CSV file with data to process. File upload fields can be rendered with the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html#method-i-file_field"><code>file_field</code></a> helper.</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">file_field</span> <span class="ss">:csv_file</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> <%= form.file_field :csv_file %> <% end %> ">Copy</button> </div> <p>The most important thing to remember with file uploads is that the rendered form's <code>enctype</code> attribute <strong>must</strong> be set to <code>multipart/form-data</code>. This is done automatically if you use a <code>file_field</code> inside a <code>form_with</code>. You can also set the attribute manually:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">url: </span><span class="s2">"/uploads"</span><span class="p">,</span> <span class="ss">multipart: </span><span class="kp">true</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">file_field_tag</span> <span class="ss">:csv_file</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with url: "/uploads", multipart: true do |form| %> <%= file_field_tag :csv_file %> <% end %> ">Copy</button> </div> <p>Both of which, output the following HTML form:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">enctype=</span><span class="s">"multipart/form-data"</span> <span class="na">action=</span><span class="s">"/people"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="c"><!-- ... --></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form enctype="multipart/form-data" action="/people" accept-charset="UTF-8" method="post"> <!-- ... --> </form> ">Copy</button> </div> <p>Note that, per <code>form_with</code> conventions, the field names in the two forms above will be different. In the first form, it will be <code>person[csv_file]</code> (accessible via <code>params[:person][:csv_file]</code>), and in the second form it will be just <code>csv_file</code> (accessible via <code>params[:csv_file]</code>).</p><h3 id="csv-file-upload-example"><a class="anchorlink" href="#csv-file-upload-example" data-turbo="false"><span>6.1</span> CSV File Upload Example</a></h3><p>When using <code>file_field</code>, the object in the <code>params</code> hash is an instance of <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionDispatch/Http/UploadedFile.html"><code>ActionDispatch::Http::UploadedFile</code></a>. Here is an example of how to save data in an uploaded CSV file to records in your application:</p><div class="interstitial code"> <pre><code class="highlight ruby"> <span class="nb">require</span> <span class="s2">"csv"</span> <span class="k">def</span> <span class="nf">upload</span> <span class="n">uploaded_file</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="ss">:csv_file</span><span class="p">]</span> <span class="k">if</span> <span class="n">uploaded_file</span><span class="p">.</span><span class="nf">present?</span> <span class="n">csv_data</span> <span class="o">=</span> <span class="no">CSV</span><span class="p">.</span><span class="nf">parse</span><span class="p">(</span><span class="n">uploaded_file</span><span class="p">.</span><span class="nf">read</span><span class="p">,</span> <span class="ss">headers: </span><span class="kp">true</span><span class="p">)</span> <span class="n">csv_data</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">row</span><span class="o">|</span> <span class="c1"># Process each row of the CSV file</span> <span class="c1"># SomeInvoiceModel.create(amount: row['Amount'], status: row['Status'])</span> <span class="no">Rails</span><span class="p">.</span><span class="nf">logger</span><span class="p">.</span><span class="nf">info</span> <span class="n">row</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1">#<CSV::Row "id":"po_1KE3FRDSYPMwkcNz9SFKuaYd" "Amount":"96.22" "Created (UTC)":"2022-01-04 02:59" "Arrival Date (UTC)":"2022-01-05 00:00" "Status":"paid"></span> <span class="k">end</span> <span class="k">end</span> <span class="c1"># ...</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text=" require "csv" def upload uploaded_file = params[:csv_file] if uploaded_file.present? csv_data = CSV.parse(uploaded_file.read, headers: true) csv_data.each do |row| # Process each row of the CSV file # SomeInvoiceModel.create(amount: row['Amount'], status: row['Status']) Rails.logger.info row.inspect #<CSV::Row "id":"po_1KE3FRDSYPMwkcNz9SFKuaYd" "Amount":"96.22" "Created (UTC)":"2022-01-04 02:59" "Arrival Date (UTC)":"2022-01-05 00:00" "Status":"paid"> end end # ... end ">Copy</button> </div> <p>If the file is an image that needs to be stored with a model (e.g. user's profile picture), there are a number of tasks to consider, like where to store the file (on Disk, Amazon S3, etc), resizing image files, and generating thumbnails, etc. <a href="active_storage_overview.html">Active Storage</a> is designed to assist with these tasks.</p><h2 id="customizing-form-builders"><a class="anchorlink" href="#customizing-form-builders" data-turbo="false"><span>7</span> Customizing Form Builders</a></h2><p>We call the objects yielded by <code>form_with</code> or <code>fields_for</code> Form Builders. Form builders allow you to generate form elements associated with a model object and are an instance of <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormBuilder.html"><code>ActionView::Helpers::FormBuilder</code></a>. This class can be extended to add custom helpers for your application.</p><p>For example, if you want to display a <code>text_field</code> along with a <code>label</code> across your application, you could add the following helper method to <code>application_helper.rb</code>:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">module</span> <span class="nn">ApplicationHelper</span> <span class="k">def</span> <span class="nf">text_field_with_label</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">attribute</span><span class="p">)</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span> <span class="o">+</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="module ApplicationHelper def text_field_with_label(form, attribute) form.label(attribute) + form.text_field(attribute) end end ">Copy</button> </div> <p>And use it in a form as usual:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">text_field_with_label</span> <span class="n">form</span><span class="p">,</span> <span class="ss">:first_name</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> <%= text_field_with_label form, :first_name %> <% end %> ">Copy</button> </div> <p>But you can also create a subclass of <code>ActionView::Helpers::FormBuilder</code>, and add the helpers there. After defining this <code>LabellingFormBuilder</code> subclass:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">LabellingFormBuilder</span> <span class="o"><</span> <span class="no">ActionView</span><span class="o">::</span><span class="no">Helpers</span><span class="o">::</span><span class="no">FormBuilder</span> <span class="k">def</span> <span class="nf">text_field</span><span class="p">(</span><span class="n">attribute</span><span class="p">,</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{})</span> <span class="c1"># super will call the original text_field method</span> <span class="n">label</span><span class="p">(</span><span class="n">attribute</span><span class="p">)</span> <span class="o">+</span> <span class="k">super</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class LabellingFormBuilder < ActionView::Helpers::FormBuilder def text_field(attribute, options = {}) # super will call the original text_field method label(attribute) + super end end ">Copy</button> </div> <p>The above form can be replaced with:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span><span class="p">,</span> <span class="ss">builder: </span><span class="no">LabellingFormBuilder</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:first_name</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person, builder: LabellingFormBuilder do |form| %> <%= form.text_field :first_name %> <% end %> ">Copy</button> </div> <p>If you reuse this frequently you could define a <code>labeled_form_with</code> helper that automatically applies the <code>builder: LabellingFormBuilder</code> option:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">module</span> <span class="nn">ApplicationHelper</span> <span class="k">def</span> <span class="nf">labeled_form_with</span><span class="p">(</span><span class="o">**</span><span class="n">options</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span> <span class="n">options</span><span class="p">[</span><span class="ss">:builder</span><span class="p">]</span> <span class="o">=</span> <span class="no">LabellingFormBuilder</span> <span class="n">form_with</span><span class="p">(</span><span class="o">**</span><span class="n">options</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="module ApplicationHelper def labeled_form_with(**options, &block) options[:builder] = LabellingFormBuilder form_with(**options, &block) end end ">Copy</button> </div> <p>The above can be used instead of <code>form_with</code>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">labeled_form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:first_name</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= labeled_form_with model: @person do |form| %> <%= form.text_field :first_name %> <% end %> ">Copy</button> </div> <p>All three cases above (the <code>text_field_with_label</code> helper, the <code>LabellingFormBuilder</code> subclass, and the <code>labeled_form_with</code> helper) will generate the same HTML output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/people"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="c"><!-- ... --></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_first_name"</span><span class="nt">></span>First name<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"person[first_name]"</span> <span class="na">id=</span><span class="s">"person_first_name"</span><span class="nt">></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/people" accept-charset="UTF-8" method="post"> <!-- ... --> <label for="person_first_name">First name</label> <input type="text" name="person[first_name]" id="person_first_name"> </form> ">Copy</button> </div> <p>The form builder used also determines what happens when you do:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">render</span> <span class="ss">partial: </span><span class="n">f</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= render partial: f %> ">Copy</button> </div> <p>If <code>f</code> is an instance of <code>ActionView::Helpers::FormBuilder</code>, then this will render the <code>form</code> partial, setting the partial's object to the form builder. If the form builder is of class <code>LabellingFormBuilder</code>, then the <code>labelling_form</code> partial would be rendered instead.</p><p>Form builder customizations, such as <code>LabellingFormBuilder</code>, do hide the implementation details (and may seem like an overkill for the simple example above). Choose between different customizations, extending <code>FormBuilder</code> class or creating helpers, based on how frequently your forms use the custom elements.</p><h2 id="form-input-naming-conventions-and-params-hash"><a class="anchorlink" href="#form-input-naming-conventions-and-params-hash" data-turbo="false"><span>8</span> Form Input Naming Conventions and <code>params</code> Hash</a></h2><p>All of the form helpers described above help with generating the HTML for form elements so that the user can enter various types of input. How do you access the user input values in the Controller? The <code>params</code> hash is the answer. You've already seen the <code>params</code> hash in the above example. This section will more explicitly go over naming conventions around how form input is structured in the <code>params</code> hash.</p><p>The <code>params</code> hash can contain arrays and arrays of hashes. Values can be at the top level of the <code>params</code> hash or nested in another hash. For example, in a standard <code>create</code> action for a Person model, <code>params[:person]</code> will be a hash of all the attributes for the <code>Person</code> object.</p><p>Note that HTML forms don't have an inherent structure to the user input data, all they generate is name-value string pairs. The arrays and hashes you see in your application are the result of parameter naming conventions that Rails uses.</p><div class="interstitial note"><p>The fields in the <code>params</code> hash need to be <a href="#permitting-parameters-in-the-controller">permitted in the controller</a>.</p></div><h3 id="basic-structure"><a class="anchorlink" href="#basic-structure" data-turbo="false"><span>8.1</span> Basic Structure</a></h3><p>The two basic structures for user input form data are arrays and hashes.</p><p>Hashes mirror the syntax used for accessing the value in <code>params</code>. For example, if a form contains:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">id=</span><span class="s">"person_name"</span> <span class="na">name=</span><span class="s">"person[name]"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">value=</span><span class="s">"Henry"</span><span class="nt">/></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input id="person_name" name="person[name]" type="text" value="Henry"/> ">Copy</button> </div> <p>the <code>params</code> hash will contain</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"name"</span> <span class="o">=></span> <span class="s2">"Henry"</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "name" => "Henry" } } ">Copy</button> </div> <p>and <code>params[:person][:name]</code> will retrieve the submitted value in the controller.</p><p>Hashes can be nested as many levels as required, for example:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">id=</span><span class="s">"person_address_city"</span> <span class="na">name=</span><span class="s">"person[address][city]"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">value=</span><span class="s">"New York"</span><span class="nt">/></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input id="person_address_city" name="person[address][city]" type="text" value="New York"/> ">Copy</button> </div> <p>The above will result in the <code>params</code> hash being</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"address"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"city"</span> <span class="o">=></span> <span class="s2">"New York"</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "address" => { "city" => "New York" } } } ">Copy</button> </div> <p>The other structure is an Array. Normally Rails ignores duplicate parameter names, but if the parameter name ends with an empty set of square brackets <code>[]</code> then the parameters will be accumulated in an Array.</p><p>For example, if you want users to be able to input multiple phone numbers, you could place this in the form:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[phone_number][]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[phone_number][]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[phone_number][]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> ">Copy</button> </div> <p>This would result in <code>params[:person][:phone_number]</code> being an array containing the submitted phone numbers:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"phone_number"</span> <span class="o">=></span> <span class="p">[</span><span class="s2">"555-0123"</span><span class="p">,</span> <span class="s2">"555-0124"</span><span class="p">,</span> <span class="s2">"555-0125"</span><span class="p">]</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "phone_number" => ["555-0123", "555-0124", "555-0125"] } } ">Copy</button> </div> <h3 id="combining-arrays-and-hashes"><a class="anchorlink" href="#combining-arrays-and-hashes" data-turbo="false"><span>8.2</span> Combining Arrays and Hashes</a></h3><p>You can mix and match these two concepts. One element of a hash might be an array as in the previous example <code>params[:person]</code> hash has a key called <code>[:phone_number]</code> whose value is an array.</p><p>You also can have an array of hashes. For example, you can create any number of addresses by repeating the following form fragment:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[addresses][][line1]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[addresses][][line2]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[addresses][][city]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[addresses][][line1]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[addresses][][line2]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"person[addresses][][city]"</span> <span class="na">type=</span><span class="s">"text"</span><span class="nt">/></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input name="person[addresses][][line1]" type="text"/> <input name="person[addresses][][line2]" type="text"/> <input name="person[addresses][][city]" type="text"/> <input name="person[addresses][][line1]" type="text"/> <input name="person[addresses][][line2]" type="text"/> <input name="person[addresses][][city]" type="text"/> ">Copy</button> </div> <p>This would result in <code>params[:person][:addresses]</code> being an array of hashes. Each hash in the array will have the keys <code>line1</code>, <code>line2</code>, and <code>city</code>, something like this:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"addresses"</span> <span class="o">=></span> <span class="p">[</span> <span class="p">{</span> <span class="s2">"line1"</span> <span class="o">=></span> <span class="s2">"1000 Fifth Avenue"</span><span class="p">,</span> <span class="s2">"line2"</span> <span class="o">=></span> <span class="s2">""</span><span class="p">,</span> <span class="s2">"city"</span> <span class="o">=></span> <span class="s2">"New York"</span> <span class="p">},</span> <span class="p">{</span> <span class="s2">"line1"</span> <span class="o">=></span> <span class="s2">"Calle de Ruiz de Alarcón"</span><span class="p">,</span> <span class="s2">"line2"</span> <span class="o">=></span> <span class="s2">""</span><span class="p">,</span> <span class="s2">"city"</span> <span class="o">=></span> <span class="s2">"Madrid"</span> <span class="p">}</span> <span class="p">]</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "addresses" => [ { "line1" => "1000 Fifth Avenue", "line2" => "", "city" => "New York" }, { "line1" => "Calle de Ruiz de Alarcón", "line2" => "", "city" => "Madrid" } ] } } ">Copy</button> </div> <p>It's important to note that while hashes can be nested arbitrarily, only one level of "arrayness" is allowed. Arrays can usually be replaced by hashes. For example, instead of an array of model objects, you can have a hash of model objects keyed by their id or similar.</p><div class="interstitial warning"><p>Array parameters do not play well with the <code>checkbox</code> helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The <code>checkbox</code> helper fakes this by creating an auxiliary hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted. If it is checked then both are submitted but the value submitted by the checkbox takes precedence. There is a <code>include_hidden</code> option that can be set to <code>false</code> if you want to omit this hidden field. By default, this option is <code>true</code>.</p></div><h3 id="hashes-with-an-index"><a class="anchorlink" href="#hashes-with-an-index" data-turbo="false"><span>8.3</span> Hashes with an Index</a></h3><p>Let's say you want to render a form with a set of fields for each of a person's addresses. The [<code>fields_for</code>][] helper with its <code>:index</code> option can assist:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">person_form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">person_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:name</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="vi">@person</span><span class="p">.</span><span class="nf">addresses</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">address</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">person_form</span><span class="p">.</span><span class="nf">fields_for</span> <span class="n">address</span><span class="p">,</span> <span class="ss">index: </span><span class="n">address</span><span class="p">.</span><span class="nf">id</span> <span class="k">do</span> <span class="o">|</span><span class="n">address_form</span><span class="o">|</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">address_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:city</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |person_form| %> <%= person_form.text_field :name %> <% @person.addresses.each do |address| %> <%= person_form.fields_for address, index: address.id do |address_form| %> <%= address_form.text_field :city %> <% end %> <% end %> <% end %> ">Copy</button> </div> <p>Assuming the person has two addresses with IDs 23 and 45, the above form would render this output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">action=</span><span class="s">"/people/1"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"_method"</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">value=</span><span class="s">"patch"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">id=</span><span class="s">"person_name"</span> <span class="na">name=</span><span class="s">"person[name]"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">id=</span><span class="s">"person_address_23_city"</span> <span class="na">name=</span><span class="s">"person[address][23][city]"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="nt">/></span> <span class="nt"><input</span> <span class="na">id=</span><span class="s">"person_address_45_city"</span> <span class="na">name=</span><span class="s">"person[address][45][city]"</span> <span class="na">type=</span><span class="s">"text"</span> <span class="nt">/></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form accept-charset="UTF-8" action="/people/1" method="post"> <input name="_method" type="hidden" value="patch" /> <input id="person_name" name="person[name]" type="text" /> <input id="person_address_23_city" name="person[address][23][city]" type="text" /> <input id="person_address_45_city" name="person[address][45][city]" type="text" /> </form> ">Copy</button> </div> <p>Which will result in a <code>params</code> hash that looks like:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"name"</span> <span class="o">=></span> <span class="s2">"Bob"</span><span class="p">,</span> <span class="s2">"address"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"23"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"city"</span> <span class="o">=></span> <span class="s2">"Paris"</span> <span class="p">},</span> <span class="s2">"45"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"city"</span> <span class="o">=></span> <span class="s2">"London"</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "name" => "Bob", "address" => { "23" => { "city" => "Paris" }, "45" => { "city" => "London" } } } } ">Copy</button> </div> <p>All of the form inputs map to the <code>"person"</code> hash because we called <code>fields_for</code> on the <code>person_form</code> form builder. Also, by specifying <code>index: address.id</code>, we rendered the <code>name</code> attribute of each city input as <code>person[address][#{address.id}][city]</code> instead of <code>person[address][city]</code>. This way you can tell which <code>Address</code> records should be modified when processing the <code>params</code> hash.</p><p>You can find more details about <code>fields_for</code> index option in the <a href="https://api.rubyonrails.org/v7.1.3.4/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for">API docs</a>.</p><h2 id="building-complex-forms"><a class="anchorlink" href="#building-complex-forms" data-turbo="false"><span>9</span> Building Complex Forms</a></h2><p>As your application grows, you may need to create more complex forms, beyond editing a single object. For example, when creating a <code>Person</code> you can allow the user to create multiple <code>Address</code> records (home, work, etc.) within the same form. When editing a <code>Person</code> record later, the user should be able to add, remove, or update addresses as well.</p><h3 id="configuring-the-model-for-nested-attributes"><a class="anchorlink" href="#configuring-the-model-for-nested-attributes" data-turbo="false"><span>9.1</span> Configuring the Model for Nested Attributes</a></h3><p>For editing an associated record for a given model (<code>Person</code> in this case), Active Record provides model level support via the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for"><code>accepts_nested_attributes_for</code></a> method:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Person</span> <span class="o"><</span> <span class="no">ApplicationRecord</span> <span class="n">has_many</span> <span class="ss">:addresses</span><span class="p">,</span> <span class="ss">inverse_of: :person</span> <span class="n">accepts_nested_attributes_for</span> <span class="ss">:addresses</span> <span class="k">end</span> <span class="k">class</span> <span class="nc">Address</span> <span class="o"><</span> <span class="no">ApplicationRecord</span> <span class="n">belongs_to</span> <span class="ss">:person</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class Person < ApplicationRecord has_many :addresses, inverse_of: :person accepts_nested_attributes_for :addresses end class Address < ApplicationRecord belongs_to :person end ">Copy</button> </div> <p>This creates an <code>addresses_attributes=</code> method on <code>Person</code> that allows you to create, update, and destroy addresses.</p><h3 id="nested-forms-in-the-view"><a class="anchorlink" href="#nested-forms-in-the-view" data-turbo="false"><span>9.2</span> Nested Forms in the View</a></h3><p>The following form allows a user to create a <code>Person</code> and its associated addresses.</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> Addresses: <span class="nt"><ul></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">fields_for</span> <span class="ss">:addresses</span> <span class="k">do</span> <span class="o">|</span><span class="n">addresses_form</span><span class="o">|</span> <span class="cp">%></span> <span class="nt"><li></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:kind</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:kind</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:street</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:street</span> <span class="cp">%></span> ... <span class="nt"></li></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> <span class="nt"></ul></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> Addresses: <ul> <%= form.fields_for :addresses do |addresses_form| %> <li> <%= addresses_form.label :kind %> <%= addresses_form.text_field :kind %> <%= addresses_form.label :street %> <%= addresses_form.text_field :street %> ... </li> <% end %> </ul> <% end %> ">Copy</button> </div> <p>When an association accepts nested attributes, <code>fields_for</code> renders its block once for every element of the association. In particular, if a person has no addresses, it renders nothing.</p><p>A common pattern is for the controller to build one or more empty children so that at least one set of fields is shown to the user. The example below would result in 2 sets of address fields being rendered on the new person form.</p><p>For example, the above <code>form_with</code> with this change:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">new</span> <span class="vi">@person</span> <span class="o">=</span> <span class="no">Person</span><span class="p">.</span><span class="nf">new</span> <span class="mi">2</span><span class="p">.</span><span class="nf">times</span> <span class="p">{</span> <span class="vi">@person</span><span class="p">.</span><span class="nf">addresses</span><span class="p">.</span><span class="nf">build</span> <span class="p">}</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="def new @person = Person.new 2.times { @person.addresses.build } end ">Copy</button> </div> <p>Will output the following HTML:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><form</span> <span class="na">action=</span><span class="s">"/people"</span> <span class="na">accept-charset=</span><span class="s">"UTF-8"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"authenticity_token"</span> <span class="na">value=</span><span class="s">"lWTbg-4_5i4rNe6ygRFowjDfTj7uf-6UPFQnsL7H9U9Fe2GGUho5PuOxfcohgm2Z-By3veuXwcwDIl-MLdwFRg"</span> <span class="na">autocomplete=</span><span class="s">"off"</span><span class="nt">></span> Addresses: <span class="nt"><ul></span> <span class="nt"><li></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_addresses_attributes_0_kind"</span><span class="nt">></span>Kind<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"person[addresses_attributes][0][kind]"</span> <span class="na">id=</span><span class="s">"person_addresses_attributes_0_kind"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_addresses_attributes_0_street"</span><span class="nt">></span>Street<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"person[addresses_attributes][0][street]"</span> <span class="na">id=</span><span class="s">"person_addresses_attributes_0_street"</span><span class="nt">></span> ... <span class="nt"></li></span> <span class="nt"><li></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_addresses_attributes_1_kind"</span><span class="nt">></span>Kind<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"person[addresses_attributes][1][kind]"</span> <span class="na">id=</span><span class="s">"person_addresses_attributes_1_kind"</span><span class="nt">></span> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"person_addresses_attributes_1_street"</span><span class="nt">></span>Street<span class="nt"></label></span> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"person[addresses_attributes][1][street]"</span> <span class="na">id=</span><span class="s">"person_addresses_attributes_1_street"</span><span class="nt">></span> ... <span class="nt"></li></span> <span class="nt"></ul></span> <span class="nt"></form></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<form action="/people" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="lWTbg-4_5i4rNe6ygRFowjDfTj7uf-6UPFQnsL7H9U9Fe2GGUho5PuOxfcohgm2Z-By3veuXwcwDIl-MLdwFRg" autocomplete="off"> Addresses: <ul> <li> <label for="person_addresses_attributes_0_kind">Kind</label> <input type="text" name="person[addresses_attributes][0][kind]" id="person_addresses_attributes_0_kind"> <label for="person_addresses_attributes_0_street">Street</label> <input type="text" name="person[addresses_attributes][0][street]" id="person_addresses_attributes_0_street"> ... </li> <li> <label for="person_addresses_attributes_1_kind">Kind</label> <input type="text" name="person[addresses_attributes][1][kind]" id="person_addresses_attributes_1_kind"> <label for="person_addresses_attributes_1_street">Street</label> <input type="text" name="person[addresses_attributes][1][street]" id="person_addresses_attributes_1_street"> ... </li> </ul> </form> ">Copy</button> </div> <p>The <code>fields_for</code> yields a form builder. The parameter names will be what <code>accepts_nested_attributes_for</code> expects. For example, when creating a person with 2 addresses, the submitted parameters in <code>params</code> would look like this:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"name"</span> <span class="o">=></span> <span class="s2">"John Doe"</span><span class="p">,</span> <span class="s2">"addresses_attributes"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"0"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"kind"</span> <span class="o">=></span> <span class="s2">"Home"</span><span class="p">,</span> <span class="s2">"street"</span> <span class="o">=></span> <span class="s2">"221b Baker Street"</span> <span class="p">},</span> <span class="s2">"1"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"kind"</span> <span class="o">=></span> <span class="s2">"Office"</span><span class="p">,</span> <span class="s2">"street"</span> <span class="o">=></span> <span class="s2">"31 Spooner Street"</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "name" => "John Doe", "addresses_attributes" => { "0" => { "kind" => "Home", "street" => "221b Baker Street" }, "1" => { "kind" => "Office", "street" => "31 Spooner Street" } } } } ">Copy</button> </div> <p>The actual value of the keys in the <code>:addresses_attributes</code> hash is not important. But they need to be strings of integers and different for each address.</p><p>If the associated object is already saved, <code>fields_for</code> autogenerates a hidden input with the <code>id</code> of the saved record. You can disable this by passing <code>include_id: false</code> to <code>fields_for</code>.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="p">{</span> <span class="s2">"person"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"name"</span> <span class="o">=></span> <span class="s2">"John Doe"</span><span class="p">,</span> <span class="s2">"addresses_attributes"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"0"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"id"</span> <span class="o">=></span> <span class="mi">1</span><span class="p">,</span> <span class="s2">"kind"</span> <span class="o">=></span> <span class="s2">"Home"</span><span class="p">,</span> <span class="s2">"street"</span> <span class="o">=></span> <span class="s2">"221b Baker Street"</span> <span class="p">},</span> <span class="s2">"1"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"id"</span> <span class="o">=></span> <span class="s2">"2"</span><span class="p">,</span> <span class="s2">"kind"</span> <span class="o">=></span> <span class="s2">"Office"</span><span class="p">,</span> <span class="s2">"street"</span> <span class="o">=></span> <span class="s2">"31 Spooner Street"</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="{ "person" => { "name" => "John Doe", "addresses_attributes" => { "0" => { "id" => 1, "kind" => "Home", "street" => "221b Baker Street" }, "1" => { "id" => "2", "kind" => "Office", "street" => "31 Spooner Street" } } } } ">Copy</button> </div> <h3 id="permitting-parameters-in-the-controller"><a class="anchorlink" href="#permitting-parameters-in-the-controller" data-turbo="false"><span>9.3</span> Permitting Parameters in the Controller</a></h3><p>As usual you need to <a href="action_controller_overview.html#strong-parameters">declare the permitted parameters</a> in the controller before you pass them to the model:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">create</span> <span class="vi">@person</span> <span class="o">=</span> <span class="no">Person</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">person_params</span><span class="p">)</span> <span class="c1"># ...</span> <span class="k">end</span> <span class="kp">private</span> <span class="k">def</span> <span class="nf">person_params</span> <span class="n">params</span><span class="p">.</span><span class="nf">expect</span><span class="p">(</span><span class="ss">person: </span><span class="p">[</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">addresses_attributes: </span><span class="p">[[</span> <span class="ss">:id</span><span class="p">,</span> <span class="ss">:kind</span><span class="p">,</span> <span class="ss">:street</span> <span class="p">]]</span> <span class="p">])</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="def create @person = Person.new(person_params) # ... end private def person_params params.expect(person: [ :name, addresses_attributes: [[ :id, :kind, :street ]] ]) end ">Copy</button> </div> <h3 id="removing-associated-objects"><a class="anchorlink" href="#removing-associated-objects" data-turbo="false"><span>9.4</span> Removing Associated Objects</a></h3><p>You can allow users to delete associated objects by passing <code>allow_destroy: true</code> to <code>accepts_nested_attributes_for</code></p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Person</span> <span class="o"><</span> <span class="no">ApplicationRecord</span> <span class="n">has_many</span> <span class="ss">:addresses</span> <span class="n">accepts_nested_attributes_for</span> <span class="ss">:addresses</span><span class="p">,</span> <span class="ss">allow_destroy: </span><span class="kp">true</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class Person < ApplicationRecord has_many :addresses accepts_nested_attributes_for :addresses, allow_destroy: true end ">Copy</button> </div> <p>If the hash of attributes for an object contains the key <code>_destroy</code> with a value that evaluates to <code>true</code> (e.g. <code>1</code>, <code>'1'</code>, <code>true</code>, or <code>'true'</code>) then the object will be destroyed. This form allows users to remove addresses:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">model: </span><span class="vi">@person</span> <span class="k">do</span> <span class="o">|</span><span class="n">form</span><span class="o">|</span> <span class="cp">%></span> Addresses: <span class="nt"><ul></span> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">fields_for</span> <span class="ss">:addresses</span> <span class="k">do</span> <span class="o">|</span><span class="n">addresses_form</span><span class="o">|</span> <span class="cp">%></span> <span class="nt"><li></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">checkbox</span> <span class="ss">:_destroy</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:kind</span> <span class="cp">%></span> <span class="cp"><%=</span> <span class="n">addresses_form</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:kind</span> <span class="cp">%></span> ... <span class="nt"></li></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> <span class="nt"></ul></span> <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with model: @person do |form| %> Addresses: <ul> <%= form.fields_for :addresses do |addresses_form| %> <li> <%= addresses_form.checkbox :_destroy %> <%= addresses_form.label :kind %> <%= addresses_form.text_field :kind %> ... </li> <% end %> </ul> <% end %> ">Copy</button> </div> <p>The HTML for the <code>_destroy</code> field:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">name=</span><span class="s">"person[addresses_attributes][0][_destroy]"</span> <span class="na">id=</span><span class="s">"person_addresses_attributes_0__destroy"</span><span class="nt">></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="checkbox" value="1" name="person[addresses_attributes][0][_destroy]" id="person_addresses_attributes_0__destroy"> ">Copy</button> </div> <p>You also need to update the permitted params in your controller to include the <code>_destroy</code> field:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">def</span> <span class="nf">person_params</span> <span class="n">params</span><span class="p">.</span><span class="nf">require</span><span class="p">(</span><span class="ss">:person</span><span class="p">).</span> <span class="nf">permit</span><span class="p">(</span><span class="ss">:name</span><span class="p">,</span> <span class="ss">addresses_attributes: </span><span class="p">[</span><span class="ss">:id</span><span class="p">,</span> <span class="ss">:kind</span><span class="p">,</span> <span class="ss">:street</span><span class="p">,</span> <span class="ss">:_destroy</span><span class="p">])</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="def person_params params.require(:person). permit(:name, addresses_attributes: [:id, :kind, :street, :_destroy]) end ">Copy</button> </div> <h3 id="preventing-empty-records"><a class="anchorlink" href="#preventing-empty-records" data-turbo="false"><span>9.5</span> Preventing Empty Records</a></h3><p>It is often useful to ignore sets of fields that the user has not filled in. You can control this by passing a <code>:reject_if</code> proc to <code>accepts_nested_attributes_for</code>. This proc will be called with each hash of attributes submitted by the form. If the proc returns <code>true</code> then Active Record will not build an associated object for that hash. The example below only tries to build an address if the <code>kind</code> attribute is set.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Person</span> <span class="o"><</span> <span class="no">ApplicationRecord</span> <span class="n">has_many</span> <span class="ss">:addresses</span> <span class="n">accepts_nested_attributes_for</span> <span class="ss">:addresses</span><span class="p">,</span> <span class="ss">reject_if: </span><span class="nb">lambda</span> <span class="p">{</span> <span class="o">|</span><span class="n">attributes</span><span class="o">|</span> <span class="n">attributes</span><span class="p">[</span><span class="s2">"kind"</span><span class="p">].</span><span class="nf">blank?</span> <span class="p">}</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class Person < ApplicationRecord has_many :addresses accepts_nested_attributes_for :addresses, reject_if: lambda { |attributes| attributes["kind"].blank? } end ">Copy</button> </div> <p>As a convenience you can instead pass the symbol <code>:all_blank</code> which will create a proc that will reject records where all the attributes are blank excluding any value for <code>_destroy</code>.</p><h2 id="forms-to-external-resources"><a class="anchorlink" href="#forms-to-external-resources" data-turbo="false"><span>10</span> Forms to External Resources</a></h2><p>Rails form helpers can be used to build a form for posting data to an external resource. If the external API expects an <code>authenticity_token</code> for the resource, this can be passed as an <code>authenticity_token: 'your_external_token'</code> parameter to <code>form_with</code>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">url: </span><span class="s1">'http://farfar.away/form'</span><span class="p">,</span> <span class="ss">authenticity_token: </span><span class="s1">'external_token'</span> <span class="k">do</span> <span class="cp">%></span> Form contents <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with url: 'http://farfar.away/form', authenticity_token: 'external_token' do %> Form contents <% end %> ">Copy</button> </div> <p>At other times, the fields that can be used in the form are limited by an external API and it may be undesirable to generate an <code>authenticity_token</code>. To <em>not</em> send a token, you can pass <code>false</code> to the <code>:authenticity_token</code> option:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_with</span> <span class="ss">url: </span><span class="s1">'http://farfar.away/form'</span><span class="p">,</span> <span class="ss">authenticity_token: </span><span class="kp">false</span> <span class="k">do</span> <span class="cp">%></span> Form contents <span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= form_with url: 'http://farfar.away/form', authenticity_token: false do %> Form contents <% end %> ">Copy</button> </div> <h2 id="using-tag-helpers-without-a-form-builder"><a class="anchorlink" href="#using-tag-helpers-without-a-form-builder" data-turbo="false"><span>11</span> Using Tag Helpers without a Form Builder</a></h2><p>In case you need to render form fields outside of the context of a form builder, Rails provides tag helpers for common form elements. For example, <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormTagHelper.html#method-i-checkbox_tag"><code>checkbox_tag</code></a>:</p><div class="interstitial code"> <pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">checkbox_tag</span> <span class="s2">"accept"</span> <span class="cp">%></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<%= checkbox_tag "accept" %> ">Copy</button> </div> <p>Output:</p><div class="interstitial code"> <pre><code class="highlight html"><span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"accept"</span> <span class="na">id=</span><span class="s">"accept"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="nt">/></span> </code></pre> <button class="clipboard-button" data-clipboard-text="<input type="checkbox" name="accept" id="accept" value="1" /> ">Copy</button> </div> <p>Generally, these helpers have the same name as their form builder counterparts plus a <code>_tag</code> suffix. For a complete list, see the <a href="https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/FormTagHelper.html"><code>FormTagHelper</code> API documentation</a>.</p><h2 id="using-form-tag-and-form-for"><a class="anchorlink" href="#using-form-tag-and-form-for" data-turbo="false"><span>12</span> Using <code>form_tag</code> and <code>form_for</code></a></h2><p>Before <code>form_with</code> was introduced in Rails 5.1 its functionality was split between <a href="https://api.rubyonrails.org/v5.2/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag"><code>form_tag</code></a> and <a href="https://api.rubyonrails.org/v5.2/classes/ActionView/Helpers/FormHelper.html#method-i-form_for"><code>form_for</code></a>. Both are now discouraged in favor of <code>form_with</code>, but you can still find being used in some codebases.</p> </section> <hr> <footer aria-labelledby="heading-feedback" role="region"> <h2 id="heading-feedback">Feedback</h2> <p> You're encouraged to help improve the quality of this guide. </p> <p> Please contribute if you see any typos or factual errors. To get started, you can read our <a href="https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section. </p> <p> You may also find incomplete content or stuff that is not up to date. Please do add any missing documentation for main. Make sure to check <a href="https://edgeguides.rubyonrails.org">Edge Guides</a> first to verify if the issues are already fixed or not on the main branch. Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a> for style and conventions. </p> <p> If for whatever reason you spot something to fix but cannot patch it yourself, please <a href="https://github.com/rails/rails/issues">open an issue</a>. </p> <p>And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome on the <a href="https://discuss.rubyonrails.org/c/rubyonrails-docs">official Ruby on Rails Forum</a>. </p> </footer> </div> </article> </main> <hr class="hide" /> <footer id="complementary"> <div class="wrapper"> <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p> <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p> </div> </footer> <a href="#main-skip-link" class="back-to-top" data-turbo="false"><span class="visibly-hidden">Back to top</span></a> </body> </html>