why file upload html not in params

Activity View Form Helpers

Forms in web applications are an essential interface for user input. Still, course markup can quickly become tedious to write and maintain because of the need to handle class control naming and its numerous attributes. Rails does away with this complexity by providing view helpers for generating form markup. However, since these helpers have different use cases, developers need to know the differences betwixt the helper methods before putting them to utilise.

Subsequently reading this guide, y'all will know:

  • How to create search forms and similar kind of generic forms not representing whatever specific model in your application.
  • How to make model-axial 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 grade different.
  • How to post forms to external resource and specify setting an authenticity_token.
  • How to build complex forms.

Capacity

  1. Dealing with Basic Forms
    • A Generic Search Form
    • Helpers for Generating Form Elements
    • Other Helpers of Involvement
  2. Dealing with Model Objects
    • Binding a Course to an Object
    • Relying on Record Identification
    • How exercise forms with PATCH, PUT, or DELETE methods work?
  3. Making Select Boxes with Ease
    • Option Groups
    • Select Boxes and Model Objects
    • Fourth dimension Zone and Land Select
  4. Using Date and Fourth dimension Class Helpers
    • Select Boxes for Individual Temporal Components
  5. Choices from a Drove of Arbitrary Objects
    • The collection_select Helper
    • The collection_radio_buttons Helper
    • The collection_check_boxes Helper
  6. Uploading Files
    • What Gets Uploaded
  7. Customizing Course Builders
  8. Understanding Parameter Naming Conventions
    • Bones Structures
    • Combining Them
    • The fields_for Helper
  9. Forms to External Resource
  10. Edifice Complex Forms
    • Configuring the Model
    • Nested Forms
    • The Controller
    • Removing Objects
    • Preventing Empty Records
    • Adding Fields on the Wing
  11. Using Tag Helpers Without a Class Builder
  12. Using form_tag and form_for

This guide is non intended to be a complete documentation of available form helpers and their arguments. Please visit the Rails API documentation for a consummate reference.

one Dealing with Basic Forms

The principal form helper is form_with.

                          <%=              form_with              practice              |              grade              |              %>              Grade contents              <%              end              %>                      

When chosen without arguments like this, it creates a form tag which, when submitted, will Post to the current page. For instance, assuming the electric current page is a abode page, the generated HTML will expect like this:

                          <form              accept-charset=              "UTF-8"              action=              "/"              method=              "post"              >              <input              proper noun=              "authenticity_token"              blazon=              "hidden"              value=              "J7CBxfHalt49OSHp27hblqK20c9PgwJ108nDHX/8Cts="              />              Course contents              </grade>                      

You'll discover that the HTML contains an input element with type subconscious. This input is important, because non-Get forms cannot be successfully submitted without it. The hidden input element with the proper noun authenticity_token is a security characteristic of Rails called cross-site asking forgery protection, and course helpers generate it for every non-Become form (provided that this security feature is enabled). You lot can read more virtually this in the Securing Rail Applications guide.

1.1 A Generic Search Form

One of the near basic forms you see on the spider web is a search form. This form contains:

  • a grade element with "GET" method,
  • a characterization for the input,
  • a text input element, and
  • a submit chemical element.

To create this form you volition use form_with and the form builder object it yields. Like and then:

                          <%=              form_with              url:                            "/search"              ,              method: :get              do              |              form              |              %>              <%=              class              .              characterization              :query              ,              "Search for:"              %>              <%=              form              .              text_field              :query              %>              <%=              form              .              submit              "Search"              %>              <%              end              %>                      

This will generate the following HTML:

                          <class              action=              "/search"              method=              "get"              have-charset=              "UTF-8"              >              <label              for=              "query"              >Search for:</characterization>              <input              id=              "query"              proper name=              "query"              type=              "text"              />              <input              proper name=              "commit"              blazon=              "submit"              value=              "Search"              data-disable-with=              "Search"              />              </form>                      

Passing url: my_specified_path to form_with tells the form where to make the asking. However, equally explained below, yous tin also laissez passer ActiveRecord objects to the form.

For every class input, an ID attribute is generated from its name ("query" in above example). These IDs can exist very useful for CSS styling or manipulation of course controls with JavaScript.

Utilize "GET" as the method for search forms. This allows users to bookmark a specific search and become back to it. More generally Rails encourages you lot to use the right HTTP verb for an activity.

1.two Helpers for Generating Form Elements

The form builder object yielded by form_with provides numerous helper methods for generating form elements such as text fields, checkboxes, and radio buttons. The first parameter to these methods is always the name of the input. When the form is submitted, the name volition be passed forth with the form data, and will make its manner to the params in the controller with the value entered by the user for that field. For example, if the course contains <%= form.text_field :query %>, so you lot would be able to get the value of this field in the controller with params[:query].

When naming inputs, Rails uses certain conventions that brand it possible to submit parameters with not-scalar values such as arrays or hashes, which volition also be accessible in params. You can read more most them in chapter Understanding Parameter Naming Conventions of this guide. For details on the precise usage of these helpers, please refer to the API documentation.

i.ii.one Checkboxes

Checkboxes are form controls that give the user a set of options they tin enable or disable:

                          <%=              form              .              check_box              :pet_dog              %>              <%=              form              .              characterization              :pet_dog              ,              "I own a dog"              %>              <%=              class              .              check_box              :pet_cat              %>              <%=              grade              .              label              :pet_cat              ,              "I own a true cat"              %>                      

This generates the post-obit:

                          <input              type=              "checkbox"              id=              "pet_dog"              name=              "pet_dog"              value=              "i"              />              <label              for=              "pet_dog"              >I own a dog</label>              <input              type=              "checkbox"              id=              "pet_cat"              proper noun=              "pet_cat"              value=              "1"              />              <characterization              for=              "pet_cat"              >I ain a cat</characterization>                      

The showtime parameter to check_box is the proper noun of the input. The second parameter is the value of the input. This value will be included in the form data (and exist present in params) when the checkbox is checked.

1.2.two Radio Buttons

Radio buttons, while similar to checkboxes, are controls that specify a set up of options in which they are mutually exclusive (i.e., the user tin can merely option ane):

                          <%=              form              .              radio_button              :age              ,              "child"              %>              <%=              form              .              label              :age_child              ,              "I am younger than 21"              %>              <%=              form              .              radio_button              :age              ,              "adult"              %>              <%=              grade              .              label              :age_adult              ,              "I am over 21"              %>                      

Output:

                          <input              type=              "radio"              id=              "age_child"              proper name=              "age"              value=              "kid"              />              <characterization              for=              "age_child"              >I am younger than 21</label>              <input              type=              "radio"              id=              "age_adult"              name=              "age"              value=              "adult"              />              <label              for=              "age_adult"              >I am over 21</label>                      

As with check_box, the second parameter to radio_button is the value of the input. Considering these two radio buttons share the same name (age), the user volition only be able to select 1 of them, and params[:age] volition contain either "child" or "developed".

Ever utilize labels for checkbox and radio buttons. They acquaintance text with a specific option and, by expanding the clickable region, make it easier for users to click the inputs.

1.3 Other Helpers of Interest

Other form controls worth mentioning are text areas, hidden fields, countersign fields, number fields, engagement and time fields, and many more:

                          <%=              form              .              text_area              :message              ,              size:                            "70x5"              %>              <%=              class              .              hidden_field              :parent_id              ,              value:                            "foo"              %>              <%=              form              .              password_field              :password              %>              <%=              form              .              number_field              :price              ,              in:                            1.0              ..              20.0              ,              step:                            0.5              %>              <%=              form              .              range_field              :disbelieve              ,              in:                            1              ..              100              %>              <%=              course              .              date_field              :born_on              %>              <%=              course              .              time_field              :started_at              %>              <%=              course              .              datetime_local_field              :graduation_day              %>              <%=              grade              .              month_field              :birthday_month              %>              <%=              grade              .              week_field              :birthday_week              %>              <%=              form              .              search_field              :name              %>              <%=              class              .              email_field              :address              %>              <%=              form              .              telephone_field              :phone              %>              <%=              form              .              url_field              :homepage              %>              <%=              form              .              color_field              :favorite_color              %>                      

Output:

                          <textarea              name=              "message"              id=              "message"              cols=              "lxx"              rows=              "5"              ></textarea>              <input              type=              "subconscious"              name=              "parent_id"              id=              "parent_id"              value=              "foo"              />              <input              type=              "password"              name=              "password"              id=              "password"              />              <input              type=              "number"              proper name=              "toll"              id=              "price"              pace=              "0.5"              min=              "one.0"              max=              "xx.0"              />              <input              type=              "range"              name=              "discount"              id=              "discount"              min=              "i"              max=              "100"              />              <input              type=              "appointment"              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=              "calendar month"              name=              "birthday_month"              id=              "birthday_month"              />              <input              blazon=              "calendar week"              name=              "birthday_week"              id=              "birthday_week"              />              <input              blazon=              "search"              name=              "proper name"              id=              "proper noun"              />              <input              type=              "e-mail"              name=              "address"              id=              "address"              />              <input              type=              "tel"              proper noun=              "phone"              id=              "phone"              />              <input              type=              "url"              name=              "homepage"              id=              "homepage"              />              <input              type=              "color"              name=              "favorite_color"              id=              "favorite_color"              value=              "#000000"              />                      

Hidden inputs are not shown to the user merely instead hold information like whatever textual input. Values inside them can be changed with JavaScript.

The search, telephone, date, time, color, datetime, datetime-local, month, week, URL, e-mail, number, and range inputs are HTML5 controls. If y'all crave your app to have a consistent experience in older browsers, you will need an HTML5 polyfill (provided by CSS and/or JavaScript). There is definitely no shortage of solutions for this, although a pop tool at the moment is Modernizr, which provides a simple way to add functionality based on the presence of detected HTML5 features.

If y'all're using password input fields (for any purpose), you might want to configure your application to prevent those parameters from beingness logged. You can learn nigh this in the Securing Rails Applications guide.

2 Dealing with Model Objects

2.1 Binding a Class to an Object

The :model argument of form_with allows us to bind the form architect object to a model object. This means that the form will be scoped to that model object, and the form's fields volition exist populated with values from that model object.

For example, if nosotros have an @article model object like:

                          @article              =              Article              .              find              (              42              )              # => #<Article id: 42, title: "My Title", body: "My Body">                      

The following form:

                          <%=              form_with              model:                            @article              do              |              form              |              %>              <%=              course              .              text_field              :title              %>              <%=              class              .              text_area              :trunk              ,              size:                            "60x10"              %>              <%=              class              .              submit              %>              <%              stop              %>                      

Outputs:

                          <form              action=              "/articles/42"              method=              "post"              accept-charset=              "UTF-8"              >              <input              name=              "authenticity_token"              type=              "subconscious"              value=              "..."              />              <input              type=              "text"              name=              "article[title]"              id=              "article_title"              value=              "My Title"              />              <textarea              name=              "commodity[body]"              id=              "article_body"              cols=              "threescore"              rows=              "ten"              >              My Trunk              </textarea>              <input              type=              "submit"              name=              "commit"              value=              "Update Article"              data-disable-with=              "Update Article"              >              </course>                      

The are several things to observe hither:

  • The form action is automatically filled with an appropriate value for @article.
  • The form fields are automatically filled with the corresponding values from @article.
  • The form field names are scoped with commodity[...]. This means that params[:article] volition be a hash containing all these field's values. You tin read more about the significance of input names in chapter Understanding Parameter Naming Conventions of this guide.
  • The submit button is automatically given an appropriate text value.

Conventionally your inputs will mirror model attributes. Still, they don't have to! If there is other information you demand you lot tin include information technology in your grade just as with attributes and access it via params[:article][:my_nifty_non_attribute_input].

ii.1.i The fields_for Helper

You can create a similar binding without really creating <grade> tags with the fields_for helper. This is useful for editing additional model objects with the aforementioned form. For example, if you had a Person model with an associated ContactDetail model, you could create a form for creating both like so:

                          <%=              form_with              model:                            @person              do              |              person_form              |              %>              <%=              person_form              .              text_field              :proper noun              %>              <%=              fields_for              :contact_detail              ,              @person              .              contact_detail              do              |              contact_detail_form              |              %>              <%=              contact_detail_form              .              text_field              :phone_number              %>              <%              end              %>              <%              cease              %>                      

which produces the following output:

                          <form              activeness=              "/people"              accept-charset=              "UTF-eight"              method=              "post"              >              <input              type=              "subconscious"              name=              "authenticity_token"              value=              "bL13x72pldyDD8bgtkjKQakJCpd4A8JdXGbfksxBDHdf1uC0kCMqe2tvVdUYfidJt0fj3ihC4NxiVHv8GVYxJA=="              />              <input              type=              "text"              name=              "person[name]"              id=              "person_name"              />              <input              type=              "text"              name=              "contact_detail[phone_number]"              id=              "contact_detail_phone_number"              />              </form>                      

The object yielded by fields_for is a form builder like the one yielded by form_with.

ii.two Relying on Record Identification

The Article model is straight available to users of the application, then - following the best practices for developing with Track - you should declare information technology a resource:

Declaring a resource has a number of side effects. See Rails Routing from the Outside In guide for more information on setting up and using resources.

When dealing with RESTful resources, calls to form_with tin go significantly easier if you rely on record identification. In short, you can just laissez passer the model case and have Rails figure out model name and the remainder. In both of these examples, the long and short fashion have the same outcome:

                          ## Creating a new article              # long-fashion:              form_with              (              model:                            @article              ,              url:                            articles_path              )              # short-style:              form_with              (              model:                            @article              )              ## Editing an existing commodity              # long-fashion:              form_with              (              model:                            @article              ,              url:                            article_path              (              @article              ),              method:                            "patch"              )              # short-way:              form_with              (              model:                            @article              )                      

Notice how the short-mode form_with invocation is conveniently the same, regardless of the record existence new or existing. Record identification is smart enough to figure out if the record is new past asking record.persisted?. It also selects the correct path to submit to, and the proper noun based on the form of the object.

If you have a singular resource, you lot volition need to call resource and resolve for it to piece of work with form_with:

                          resource              :geocoder              resolve              (              'Geocoder'              )              {              [              :geocoder              ]              }                      

When y'all're using STI (single-table inheritance) with your models, you can't rely on tape identification on a subclass if only their parent class is declared a resource. Yous will have to specify :url, and :scope (the model proper noun) explicitly.

ii.two.1 Dealing with Namespaces

If you lot have created namespaced routes, form_with has a nifty shorthand for that too. If your application has an admin namespace and so

                          form_with              model:                            [              :admin              ,              @commodity              ]                      

will create a form that submits to the ArticlesController inside the admin namespace (submitting to admin_article_path(@article) in the case of an update). If you lot accept several levels of namespacing then the syntax is like:

                          form_with              model:                            [              :admin              ,              :direction              ,              @article              ]                      

For more than information on Rails' routing system and the associated conventions, delight run into Rails Routing from the Outside In guide.

2.three How do forms with PATCH, PUT, or DELETE methods work?

The Rails framework encourages RESTful design of your applications, which means you'll exist making a lot of "PATCH", "PUT", and "DELETE" requests (besides "GET" and "Postal service"). Notwithstanding, most browsers don't support methods other than "Get" and "POST" when it comes to submitting forms.

Rails works around this result past emulating other methods over Mail with a hidden input named "_method", which is set to reflect the desired method:

                          form_with              (              url:                            search_path              ,              method:                            "patch"              )                      

Output:

                          <form              accept-charset=              "UTF-8"              action=              "/search"              method=              "post"              >              <input              name=              "_method"              type=              "hidden"              value=              "patch"              />              <input              name=              "authenticity_token"              type=              "hidden"              value=              "f755bb0ed134b76c432144748a6d4b7a7ddf2b71"              />              <!-- ... -->              </class>                      

When parsing POSTed data, Rail will have into business relationship the special _method parameter and act as if the HTTP method was the one specified inside information technology ("PATCH" in this example).

When rendering a grade, submission buttons tin override the declared method attribute through the formmethod: keyword:

                          <%=              form_with              url:                            "/posts/one"              ,              method: :patch              do              |              grade              |              %>              <%=              form              .              push button              "Delete"              ,              formmethod: :delete              ,              data:                            {              ostend:                            "Are you sure?"              }              %>              <%=              form              .              button              "Update"              %>              <%              end              %>                      

Like to <form> elements, about browsers don't back up overriding form methods declared through formmethod other than "Get" and "Mail".

Rails works around this issue by emulating other methods over Post through a combination of formmethod, value, and proper noun attributes:

                          <grade              accept-charset=              "UTF-eight"              action=              "/posts/1"              method=              "post"              >              <input              proper name=              "_method"              type=              "hidden"              value=              "patch"              />              <input              proper name=              "authenticity_token"              type=              "hidden"              value=              "f755bb0ed134b76c432144748a6d4b7a7ddf2b71"              />              <!-- ... -->              <push button              type=              "submit"              formmethod=              "post"              name=              "_method"              value=              "delete"              data-ostend=              "Are you certain?"              >Delete</button>              <push              type=              "submit"              name=              "button"              >Update</button>              </grade>                      

In Rails 6.0 and 5.2, all forms using form_with implement remote: true by default. These forms will submit data using an XHR (Ajax) request. To disable this include local: truthful. To dive deeper run across Working with JavaScript in Rail guide.

3 Making Select Boxes with Ease

Select boxes in HTML require a significant amount of markup - one <option> element for each option to choose from. And so Rails provides helper methods to reduce this burden.

For example, let's say we have a list of cities for the user to choose from. We tin can apply the select helper like so:

                          <%=              class              .              select              :city              ,              [              "Berlin"              ,              "Chicago"              ,              "Madrid"              ]              %>                      

Output:

                          <select              proper name=              "city"              id=              "urban center"              >              <option              value=              "Berlin"              >Berlin</option>              <option              value=              "Chicago"              >Chicago</option>              <option              value=              "Madrid"              >Madrid</option>              </select>                      

We can also designate <selection> values that differ from their labels:

                          <%=              class              .              select              :metropolis              ,              [[              "Berlin"              ,              "BE"              ],              [              "Chicago"              ,              "CHI"              ],              [              "Madrid"              ,              "Doctor"              ]]              %>                      

Output:

                          <select              proper name=              "metropolis"              id=              "metropolis"              >              <option              value=              "BE"              >Berlin</pick>              <option              value=              "CHI"              >Chicago</selection>              <selection              value=              "MD"              >Madrid</option>              </select>                      

This fashion, the user will see the total metropolis proper name, simply params[:city] volition be one of "BE", "CHI", or "Medico".

Lastly, we can specify a default option for the select box with the :selected argument:

                          <%=              class              .              select              :city              ,              [[              "Berlin"              ,              "BE"              ],              [              "Chicago"              ,              "CHI"              ],              [              "Madrid"              ,              "MD"              ]],              selected:                            "CHI"              %>                      

Output:

                          <select              name=              "urban center"              id=              "city"              >              <option              value=              "Be"              >Berlin</option>              <pick              value=              "CHI"              selected=              "selected"              >Chicago</option>              <option              value=              "Doc"              >Madrid</choice>              </select>                      

3.1 Option Groups

In some cases we may want to improve the user feel by group related options together. Nosotros can do and so by passing a Hash (or comparable Assortment) to select:

                          <%=              form              .              select              :metropolis              ,              {              "Europe"              =>              [              [              "Berlin"              ,              "BE"              ],              [              "Madrid"              ,              "MD"              ]              ],              "North America"              =>              [              [              "Chicago"              ,              "CHI"              ]              ],              },              selected:                            "CHI"              %>                      

Output:

                          <select              proper noun=              "urban center"              id=              "city"              >              <optgroup              characterization=              "Europe"              >              <option              value=              "Be"              >Berlin</option>              <pick              value=              "MD"              >Madrid</option>              </optgroup>              <optgroup              label=              "North America"              >              <option              value=              "CHI"              selected=              "selected"              >Chicago</option>              </optgroup>              </select>                      

iii.two Select Boxes and Model Objects

Like other class controls, a select box tin can be leap to a model aspect. For instance, if nosotros have a @person model object like:

                          @person              =              Person              .              new              (              urban center:                            "Physician"              )                      

The following class:

                          <%=              form_with              model:                            @person              do              |              form              |              %>              <%=              form              .              select              :city              ,              [[              "Berlin"              ,              "BE"              ],              [              "Chicago"              ,              "CHI"              ],              [              "Madrid"              ,              "MD"              ]]              %>              <%              end              %>                      

Outputs a select box like:

                          <select              name=              "person[city]"              id=              "person_city"              >              <pick              value=              "BE"              >Berlin</option>              <pick              value=              "CHI"              >Chicago</option>              <option              value=              "Physician"              selected=              "selected"              >Madrid</option>              </select>                      

Notice that the advisable selection was automatically marked selected="selected". Since this select box was bound to a model, nosotros didn't need to specify a :selected statement!

3.iii Time Zone and Country Select

To leverage time zone support in Rails, you take to ask your users what time zone they are in. Doing and so would crave generating select options from a list of pre-divers ActiveSupport::TimeZone objects, just you lot can simply use the time_zone_select helper that already wraps this:

                          <%=              form              .              time_zone_select              :time_zone              %>                      

Runway used to have a country_select helper for choosing countries, just this has been extracted to the country_select plugin.

iv Using Date and Fourth dimension Grade Helpers

If you practice not wish to utilize HTML5 date and fourth dimension inputs, Track provides alternative appointment and fourth dimension form helpers that render plain select boxes. These helpers render a select box for each temporal component (due east.g. year, month, day, etc). For example, if we have a @person model object like:

                          @person              =              Person              .              new              (              birth_date:                            Date              .              new              (              1995              ,              12              ,              21              ))                      

The following form:

                          <%=              form_with              model:                            @person              do              |              grade              |              %>              <%=              form              .              date_select              :birth_date              %>              <%              end              %>                      

Outputs select boxes like:

                          <select              name=              "person[birth_date(1i)]"              id=              "person_birth_date_1i"              >              <selection              value=              "1990"              >1990</option>              <option              value=              "1991"              >1991</option>              <selection              value=              "1992"              >1992</option>              <option              value=              "1993"              >1993</pick>              <option              value=              "1994"              >1994</option>              <option              value=              "1995"              selected=              "selected"              >1995</option>              <option              value=              "1996"              >1996</option>              <option              value=              "1997"              >1997</option>              <selection              value=              "1998"              >1998</choice>              <selection              value=              "1999"              >1999</selection>              <selection              value=              "2000"              >2000</choice>              </select>              <select              name=              "person[birth_date(2i)]"              id=              "person_birth_date_2i"              >              <selection              value=              "1"              >January</option>              <option              value=              "2"              >February</selection>              <option              value=              "three"              >March</option>              <option              value=              "4"              >April</option>              <pick              value=              "v"              >May</selection>              <pick              value=              "6"              >June</option>              <selection              value=              "7"              >July</pick>              <option              value=              "viii"              >Baronial</pick>              <option              value=              "ix"              >September</selection>              <option              value=              "10"              >Oct</pick>              <option              value=              "11"              >November</choice>              <selection              value=              "12"              selected=              "selected"              >December</pick>              </select>              <select              name=              "person[birth_date(3i)]"              id=              "person_birth_date_3i"              >              <selection              value=              "i"              >ane</option>              ...              <selection              value=              "21"              selected=              "selected"              >21</option>              ...              <option              value=              "31"              >31</option>              </select>                      

Notice that, when the grade is submitted, there will be no unmarried value in the params hash that contains the full date. Instead, there will be several values with special names like "birth_date(1i)". Agile Record knows how to gather these specially-named values into a full date or time, based on the declared type of the model attribute. And so we can pass params[:person] to e.g. Person.new or Person#update merely like nosotros would if the form used a unmarried field to represent the full appointment.

In addition to the date_select helper, Rails provides time_select and datetime_select.

4.1 Select Boxes for Individual Temporal Components

Rails also provides helpers to render select boxes for individual temporal components: select_year, select_month, select_day, select_hour, select_minute, and select_second. These helpers are "bare" methods, meaning they are non called on a form builder instance. For example:

                          <%=              select_year              1999              ,              prefix:                            "party"              %>                      

Outputs a select box like:

                          <select              name=              "political party[twelvemonth]"              id=              "party_year"              >              <option              value=              "1994"              >1994</pick>              <selection              value=              "1995"              >1995</pick>              <pick              value=              "1996"              >1996</pick>              <option              value=              "1997"              >1997</option>              <selection              value=              "1998"              >1998</option>              <option              value=              "1999"              selected=              "selected"              >1999</option>              <choice              value=              "2000"              >2000</option>              <option              value=              "2001"              >2001</option>              <option              value=              "2002"              >2002</option>              <pick              value=              "2003"              >2003</option>              <option              value=              "2004"              >2004</selection>              </select>                      

For each of these helpers, yous may specify a date or time object instead of a number every bit the default value, and the appropriate temporal component volition be extracted and used.

5 Choices from a Drove of Arbitrary Objects

Often, nosotros want to generate a ready of choices in a class from a collection of objects. For case, when nosotros want the user to cull from cities in our database, and we have a City model like:

                          Urban center              .              club              (              :name              ).              to_a              # => [              #      #<City id: iii, proper noun: "Berlin">,              #      #<Urban center id: 1, name: "Chicago">,              #      #<City id: 2, name: "Madrid">              #    ]                      

Rail provides helpers that generate choices from a collection without having to explicitly iterate over it. These helpers make up one's mind the value and text label of each choice by calling specified methods on each object in the collection.

5.1 The collection_select Helper

To generate a select box for our cities, we can use collection_select:

                          <%=              form              .              collection_select              :city_id              ,              City              .              lodge              (              :name              ),              :id              ,              :name              %>                      

Output:

                          <select              proper noun=              "city_id"              id=              "city_id"              >              <option              value=              "3"              >Berlin</option>              <option              value=              "1"              >Chicago</option>              <pick              value=              "2"              >Madrid</option>              </select>                      

With collection_select we specify the value method showtime (:id in the example above), and the text label method second (:name in the example in a higher place). This is contrary of the order used when specifying choices for the select helper, where the text characterization comes first and the value second.

v.2 The collection_radio_buttons Helper

To generate a set of radio buttons for our cities, we can use collection_radio_buttons:

                          <%=              form              .              collection_radio_buttons              :city_id              ,              City              .              social club              (              :name              ),              :id              ,              :proper noun              %>                      

Output:

                          <input              type=              "radio"              proper noun=              "city_id"              value=              "3"              id=              "city_id_3"              >              <characterization              for=              "city_id_3"              >Berlin</characterization>              <input              type=              "radio"              name=              "city_id"              value=              "1"              id=              "city_id_1"              >              <label              for=              "city_id_1"              >Chicago</label>              <input              type=              "radio"              name=              "city_id"              value=              "ii"              id=              "city_id_2"              >              <label              for=              "city_id_2"              >Madrid</label>                      

v.3 The collection_check_boxes Helper

To generate a fix of cheque boxes for our cities (which allows users to choose more 1), we can use collection_check_boxes:

                          <%=              course              .              collection_check_boxes              :city_id              ,              City              .              gild              (              :name              ),              :id              ,              :proper noun              %>                      

Output:

                          <input              type=              "checkbox"              proper name=              "city_id[]"              value=              "3"              id=              "city_id_3"              >              <characterization              for=              "city_id_3"              >Berlin</label>              <input              type=              "checkbox"              name=              "city_id[]"              value=              "1"              id=              "city_id_1"              >              <characterization              for=              "city_id_1"              >Chicago</label>              <input              blazon=              "checkbox"              name=              "city_id[]"              value=              "2"              id=              "city_id_2"              >              <label              for=              "city_id_2"              >Madrid</label>                      

6 Uploading Files

A mutual chore is uploading some sort of file, whether information technology's a moving-picture show of a person or a CSV file containing data to process. File upload fields can be rendered with the file_field helper.

                          <%=              form_with              model:                            @person              do              |              form              |              %>              <%=              form              .              file_field              :picture              %>              <%              terminate              %>                      

The most of import thing to recall with file uploads is that the rendered class's enctype aspect must be gear up to "multipart/form-data". This is done automatically if you utilize a file_field within a form_with. You tin can also set the attribute manually:

                          <%=              form_with              url:                            "/uploads"              ,              multipart:                            true              exercise              |              form              |              %>              <%=              file_field_tag              :picture              %>              <%              end              %>                      

Note that, in accordance with form_with conventions, the field names in the two forms higher up volition also differ. That is, the field name in the first course will be person[picture] (accessible via params[:person][:picture]), and the field proper noun in the second course will exist just moving-picture show (accessible via params[:picture]).

6.one What Gets Uploaded

The object in the params hash is an instance of ActionDispatch::Http::UploadedFile. The following snippet saves the uploaded file in #{Rails.root}/public/uploads nether the same name as the original file.

                          def              upload              uploaded_file              =              params              [              :pic              ]              File              .              open up              (              Track              .              root              .              join              (              'public'              ,              'uploads'              ,              uploaded_file              .              original_filename              ),              'wb'              )              exercise              |              file              |              file              .              write              (              uploaded_file              .              read              )              end              cease                      

Once a file has been uploaded, there are a multitude of potential tasks, ranging from where to store the files (on Disk, Amazon S3, etc), associating them with models, resizing prototype files, and generating thumbnails, etc. Active Storage is designed to assist with these tasks.

7 Customizing Class Builders

The object yielded by form_with and fields_for is an instance of ActionView::Helpers::FormBuilder. Grade builders encapsulate the notion of displaying form elements for a single object. While you tin can write helpers for your forms in the usual way, you can too create a subclass of ActionView::Helpers::FormBuilder, and add together the helpers in that location. For example,

                          <%=              form_with              model:                            @person              do              |              form              |              %>              <%=              text_field_with_label              form              ,              :first_name              %>              <%              end              %>                      

can be replaced with

                          <%=              form_with              model:                            @person              ,              builder:                            LabellingFormBuilder              practice              |              grade              |              %>              <%=              form              .              text_field              :first_name              %>              <%              stop              %>                      

by defining a LabellingFormBuilder class similar to the post-obit:

                          class              LabellingFormBuilder              <              ActionView              ::              Helpers              ::              FormBuilder              def              text_field              (              aspect              ,              options              =              {})              label              (              attribute              )              +              super              terminate              end                      

If y'all reuse this frequently you could ascertain a labeled_form_with helper that automatically applies the builder: LabellingFormBuilder option:

                          def              labeled_form_with              (              model:                            zilch              ,              scope:                            nil              ,              url:                            nil              ,              format:                            cypher              ,              **              options              ,              &              block              )              options              .              merge!              builder:                            LabellingFormBuilder              form_with              model:                            model              ,              scope:                            scope              ,              url:                            url              ,              format:                            format              ,              **              options              ,              &              block              end                      

The form builder used too determines what happens when you exercise:

If f is an instance of ActionView::Helpers::FormBuilder, then this volition return the grade partial, setting the partial's object to the form builder. If the form builder is of course LabellingFormBuilder, then the labelling_form fractional would be rendered instead.

8 Understanding Parameter Naming Conventions

Values from forms can be at the pinnacle level of the params hash or nested in another hash. For example, in a standard create action for a Person model, params[:person] would unremarkably be a hash of all the attributes for the person to create. The params hash can also contain arrays, arrays of hashes, and so on.

Fundamentally HTML forms don't know about any sort of structured data, all they generate is proper noun-value pairs, where pairs are but plain strings. The arrays and hashes you see in your application are the upshot of some parameter naming conventions that Track uses.

8.ane Basic Structures

The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in params. For instance, if a form contains:

                          <input              id=              "person_name"              name=              "person[name]"              type=              "text"              value=              "Henry"              />                      

the params hash will contain

                          {              'person'              =>              {              'name'              =>              'Henry'              }}                      

and params[:person][:name] volition retrieve the submitted value in the controller.

Hashes can be nested every bit many levels every bit required, for example:

                          <input              id=              "person_address_city"              name=              "person[accost][city]"              blazon=              "text"              value=              "New York"              />                      

will result in the params hash being

                          {              'person'              =>              {              'address'              =>              {              'city'              =>              'New York'              }}}                      

Unremarkably Rails ignores indistinguishable parameter names. If the parameter proper name ends with an empty set of square brackets [] then they volition be accumulated in an array. If you wanted users to be able to input multiple phone numbers, y'all could place this in the grade:

                          <input              name=              "person[phone_number][]"              type=              "text"              />              <input              proper name=              "person[phone_number][]"              type=              "text"              />              <input              name=              "person[phone_number][]"              type=              "text"              />                      

This would upshot in params[:person][:phone_number] being an array containing the inputted phone numbers.

8.2 Combining Them

We can mix and match these 2 concepts. Ane element of a hash might be an array as in the previous instance, or you tin have an array of hashes. For example, a form might let you create any number of addresses by repeating the following form fragment

                          <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"              />                      

This would result in params[:person][:addresses] existence an array of hashes with keys line1, line2, and city.

In that location'southward a restriction, even so: while hashes can exist nested arbitrarily, only one level of "arrayness" is allowed. Arrays can ordinarily be replaced past hashes; for example, instead of having an array of model objects, 1 tin can take a hash of model objects keyed by their id, an assortment index, or another parameter.

Array parameters exercise non play well with the check_box helper. According to the HTML specification unchecked checkboxes submit no value. However it is frequently user-friendly for a checkbox to always submit a value. The check_box helper fakes this by creating an auxiliary hidden input with the same proper name. If the checkbox is unchecked simply the hidden input is submitted and if it is checked then both are submitted only the value submitted by the checkbox takes precedence.

8.3 The fields_for Helper

Permit's say nosotros want to render a form with a prepare of fields for each of a person's addresses. The fields_for helper and its :index argument tin can assist with this:

                          <%=              form_with              model:                            @person              do              |              person_form              |              %>              <%=              person_form              .              text_field              :proper name              %>              <%              @person              .              addresses              .              each              do              |              address              |              %>              <%=              person_form              .              fields_for              address              ,              index:                            address              .              id              practise              |              address_form              |              %>              <%=              address_form              .              text_field              :metropolis              %>              <%              end              %>              <%              end              %>              <%              end              %>                      

Assuming the person had ii addresses, with ids 23 and 45 this would create output like to this:

                          <form              take-charset=              "UTF-eight"              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[accost][23][city]"              type=              "text"              />              <input              id=              "person_address_45_city"              proper noun=              "person[address][45][city]"              type=              "text"              />              </grade>                      

This will result in a params hash that looks like

                          {              'person'              =>              {              'name'              =>              'Bob'              ,              'address'              =>              {              '23'              =>              {              'city'              =>              'Paris'              },              '45'              =>              {              'metropolis'              =>              'London'              }}}}                      

Track knows that all these inputs should be part of the person hash because you called fields_for on the first form architect. Past specifying an :index option y'all're telling Rails that instead of naming the inputs person[address][urban center] it should insert that index surrounded past [] betwixt the address and the metropolis. This is often useful as it is then easy to locate which Address record should be modified. Yous tin laissez passer numbers with some other significance, strings or fifty-fifty cipher (which volition result in an array parameter existence created).

To create more intricate nestings, you can specify the first part of the input proper noun (person[accost] in the previous example) explicitly:

                          <%=              fields_for              'person[address][primary]'              ,              accost              ,              index:                            address              .              id              do              |              address_form              |              %>              <%=              address_form              .              text_field              :city              %>              <%              cease              %>                      

volition create inputs like

                          <input              id=              "person_address_primary_1_city"              proper noun=              "person[accost][primary][1][city]"              blazon=              "text"              value=              "Bologna"              />                      

As a full general dominion the final input name is the concatenation of the name given to fields_for/form_with, the index value, and the name of the aspect. You can as well laissez passer an :index option directly to helpers such equally text_field, but it is usually less repetitive to specify this at the course architect level rather than on private input controls.

Every bit a shortcut you lot tin can append [] to the name and omit the :index option. This is the aforementioned as specifying index: address.id so

                          <%=              fields_for              'person[address][main][]'              ,              address              exercise              |              address_form              |              %>              <%=              address_form              .              text_field              :city              %>              <%              end              %>                      

produces exactly the same output equally the previous example.

9 Forms to External Resource

Runway' form helpers can likewise be used to build a form for posting data to an external resource. Nevertheless, at times information technology can be necessary to set an authenticity_token for the resource; this can be done past passing an authenticity_token: 'your_external_token' parameter to the form_with options:

                          <%=              form_with              url:                            'http://farfar.away/class'              ,              authenticity_token:                            'external_token'              practise              %>              Grade contents              <%              finish              %>                      

Sometimes when submitting data to an external resource, similar a payment gateway, the fields that can be used in the form are express by an external API and information technology may be undesirable to generate an authenticity_token. To not send a token, simply pass false to the :authenticity_token pick:

                          <%=              form_with              url:                            'http://farfar.away/form'              ,              authenticity_token:                            faux              do              %>              Grade contents              <%              terminate              %>                      

10 Edifice Circuitous Forms

Many apps grow beyond simple forms editing a single object. For example, when creating a Person you might want to let the user to (on the aforementioned form) create multiple accost records (dwelling, work, etc.). When later editing that person the user should be able to add, remove, or amend addresses every bit necessary.

10.1 Configuring the Model

Agile Record provides model level back up via the accepts_nested_attributes_for method:

                          grade              Person              <              ApplicationRecord              has_many              :addresses              ,              inverse_of: :person              accepts_nested_attributes_for              :addresses              stop              class              Address              <              ApplicationRecord              belongs_to              :person              end                      

This creates an addresses_attributes= method on Person that allows you to create, update, and (optionally) destroy addresses.

10.ii Nested Forms

The following class allows a user to create a Person and its associated addresses.

                          <%=              form_with              model:                            @person              practice              |              class              |              %>              Addresses:              <ul>              <%=              form              .              fields_for              :addresses              do              |              addresses_form              |              %>              <li>              <%=              addresses_form              .              characterization              :kind              %>              <%=              addresses_form              .              text_field              :kind              %>              <%=              addresses_form              .              label              :street              %>              <%=              addresses_form              .              text_field              :street              %>              ...              </li>              <%              end              %>              </ul>              <%              end              %>                      

When an association accepts nested attributes fields_for renders its cake once for every element of the association. In particular, if a person has no addresses it renders nothing. A common design is for the controller to build 1 or more empty children so that at to the lowest degree 1 prepare of fields is shown to the user. The example below would result in 2 sets of address fields beingness rendered on the new person course.

                          def              new              @person              =              Person              .              new              2              .              times              {              @person              .              addresses              .              build              }              end                      

The fields_for yields a form builder. The parameters' name volition be what accepts_nested_attributes_for expects. For instance, when creating a user with 2 addresses, the submitted parameters would expect like:

                          {              'person'              =>              {              'name'              =>              'John Doe'              ,              'addresses_attributes'              =>              {              '0'              =>              {              'kind'              =>              'Home'              ,              'street'              =>              '221b Baker Street'              },              'ane'              =>              {              'kind'              =>              'Office'              ,              'street'              =>              '31 Spooner Street'              }              }              }              }                      

The keys of the :addresses_attributes hash are unimportant, they demand merely be different for each address.

If the associated object is already saved, fields_for autogenerates a hidden input with the id of the saved record. You tin disable this by passing include_id: false to fields_for.

10.3 The Controller

Every bit usual you lot need to declare the permitted parameters in the controller before yous pass them to the model:

                          def              create              @person              =              Person              .              new              (              person_params              )              # ...              end              private              def              person_params              params              .              crave              (              :person              ).              permit              (              :name              ,              addresses_attributes:                            [              :id              ,              :kind              ,              :street              ])              end                      

10.4 Removing Objects

You tin allow users to delete associated objects by passing allow_destroy: truthful to accepts_nested_attributes_for

                          class              Person              <              ApplicationRecord              has_many              :addresses              accepts_nested_attributes_for              :addresses              ,              allow_destroy:                            true              end                      

If the hash of attributes for an object contains the primal _destroy with a value that evaluates to true (eastward.grand. 1, '1', true, or 'truthful') and then the object will be destroyed. This course allows users to remove addresses:

                          <%=              form_with              model:                            @person              practise              |              form              |              %>              Addresses:              <ul>              <%=              class              .              fields_for              :addresses              practice              |              addresses_form              |              %>              <li>              <%=              addresses_form              .              check_box              :_destroy              %>              <%=              addresses_form              .              label              :kind              %>              <%=              addresses_form              .              text_field              :kind              %>              ...              </li>              <%              end              %>              </ul>              <%              end              %>                      

Don't forget to update the permitted params in your controller to likewise include the _destroy field:

                          def              person_params              params              .              crave              (              :person              ).              allow              (              :name              ,              addresses_attributes:                            [              :id              ,              :kind              ,              :street              ,              :_destroy              ])              end                      

10.5 Preventing Empty Records

It is often useful to ignore sets of fields that the user has not filled in. You can control this by passing a :reject_if proc to accepts_nested_attributes_for. This proc will be chosen with each hash of attributes submitted by the course. If the proc returns true and then Agile Record will not build an associated object for that hash. The example below only tries to build an accost if the kind attribute is ready.

                          form              Person              <              ApplicationRecord              has_many              :addresses              accepts_nested_attributes_for              :addresses              ,              reject_if:                            lambda              {              |              attributes              |              attributes              [              'kind'              ].              bare?              }              end                      

As a convenience you lot can instead pass the symbol :all_blank which will create a proc that volition reject records where all the attributes are blank excluding any value for _destroy.

10.6 Adding Fields on the Fly

Rather than rendering multiple sets of fields ahead of time you may wish to add them only when a user clicks on an "Add new address" button. Rails does not provide any built-in support for this. When generating new sets of fields you must ensure the key of the associated assortment is unique - the current JavaScript date (milliseconds since the epoch) is a common pick.

11 Using Tag Helpers Without a Form Builder

In example yous need to return form fields exterior of the context of a form builder, Rails provides tag helpers for mutual form elements. For example, check_box_tag:

                          <%=              check_box_tag              "have"              %>                      

Output:

                          <input              type=              "checkbox"              proper noun=              "have"              id=              "accept"              value=              "i"              />                      

Generally, these helpers have the same name as their form builder counterparts plus a _tag suffix. For a complete list, see the FormTagHelper API documentation.

12 Using form_tag and form_for

Before form_with was introduced in Rail 5.1 its functionality used to be split betwixt form_tag and form_for. Both are now soft-deprecated. Documentation on their usage can exist constitute in older versions of this guide.

Feedback

You're encouraged to assist improve the quality of this guide.

Please contribute if yous see any typos or factual errors. To get started, yous can read our documentation contributions department.

You lot may also find incomplete content or stuff that is not up to appointment. Delight do add together any missing documentation for main. Brand sure to check Edge Guides get-go to verify if the issues are already stock-still or not on the main branch. Bank check the Ruby on Rails Guides Guidelines for style and conventions.

If for whatever reason y'all spot something to fix but cannot patch it yourself, please open up an issue.

And last but not least, any kind of discussion regarding Blood-red on Rails documentation is very welcome on the rubyonrails-docs mailing list.

burrisprepaing1940.blogspot.com

Source: https://guides.rubyonrails.org/form_helpers.html

0 Response to "why file upload html not in params"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel