HEX
Server: Apache
System: Linux s198.coreserver.jp 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: nagasaki (10062)
PHP: 7.1.33
Disabled: NONE
Upload Files
File: //usr/local/rvm/gems/default/doc/actionview-6.1.4.1/ri/ActionView/Helpers/FormHelper/form_with-i.ri
U:RDoc::AnyMethod[iI"form_with:ETI".ActionView::Helpers::FormHelper#form_with;TF:publico:RDoc::Markup::Document:@parts[Xo:RDoc::Markup::Paragraph;	[I"@Creates a form tag based on mixing URLs, scopes, or models.;To:RDoc::Markup::BlankLineo:RDoc::Markup::Verbatim;	[4I"# Using just a URL:
;TI"0<%= form_with url: posts_path do |form| %>
;TI"%  <%= form.text_field :title %>
;TI"<% end %>
;TI"
# =>
;TI"=<form action="/posts" method="post" data-remote="true">
;TI"(  <input type="text" name="title">
;TI"
</form>
;TI"
;TI"6# Adding a scope prefixes the input field names:
;TI"><%= form_with scope: :post, url: posts_path do |form| %>
;TI"%  <%= form.text_field :title %>
;TI"<% end %>
;TI"
# =>
;TI"=<form action="/posts" method="post" data-remote="true">
;TI".  <input type="text" name="post[title]">
;TI"
</form>
;TI"
;TI"4# Using a model infers both the URL and scope:
;TI"0<%= form_with model: Post.new do |form| %>
;TI"%  <%= form.text_field :title %>
;TI"<% end %>
;TI"
# =>
;TI"=<form action="/posts" method="post" data-remote="true">
;TI".  <input type="text" name="post[title]">
;TI"
</form>
;TI"
;TI"J# An existing model makes an update form and fills out field values:
;TI"2<%= form_with model: Post.first do |form| %>
;TI"%  <%= form.text_field :title %>
;TI"<% end %>
;TI"
# =>
;TI"?<form action="/posts/1" method="post" data-remote="true">
;TI":  <input type="hidden" name="_method" value="patch">
;TI"N  <input type="text" name="post[title]" value="<the title of the post>">
;TI"
</form>
;TI"
;TI"G# Though the fields don't have to correspond to model attributes:
;TI"/<%= form_with model: Cat.new do |form| %>
;TI"4  <%= form.text_field :cats_dont_have_gills %>
;TI"5  <%= form.text_field :but_in_forms_they_can %>
;TI"<% end %>
;TI"
# =>
;TI"<<form action="/cats" method="post" data-remote="true">
;TI"<  <input type="text" name="cat[cats_dont_have_gills]">
;TI"=  <input type="text" name="cat[but_in_forms_they_can]">
;TI"
</form>
;T:@format0o;
;	[	I"LThe parameters in the forms are accessible in controllers according to ;TI"Ntheir name nesting. So inputs named +title+ and <tt>post[title]</tt> are ;TI"Naccessible as <tt>params[:title]</tt> and <tt>params[:post][:title]</tt> ;TI"respectively.;T@o;
;	[	I"KFor ease of comparison the examples above left out the submit button, ;TI"Kas well as the auto generated hidden fields that enable UTF-8 support ;TI"Jand adds an authenticity token needed for cross site request forgery ;TI"protection.;T@S:RDoc::Markup::Heading:
leveli:	textI"Resource-oriented style;T@o;
;	[I"LIn many of the examples just shown, the +:model+ passed to +form_with+ ;TI"Mis a _resource_. It corresponds to a set of RESTful routes, most likely ;TI":defined via +resources+ in <tt>config/routes.rb</tt>.;T@o;
;	[I"JSo when passing such a model record, Rails infers the URL and method.;T@o;;	[I"-<%= form_with model: @post do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0o;
;	[I"*is then equivalent to something like:;T@o;;	[I"T<%= form_with scope: :post, url: post_path(@post), method: :patch do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0o;
;	[I"And for a new record;T@o;;	[I"0<%= form_with model: Post.new do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0o;
;	[I"%is equivalent to something like:;T@o;;	[I"><%= form_with scope: :post, url: posts_path do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0S;;i	;I"+form_with+ options;T@o:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;	[o;
;	[	I"K<tt>:url</tt> - The URL the form submits to. Akin to values passed to ;TI"D+url_for+ or +link_to+. For example, you may use a named route ;TI"Ldirectly. When a <tt>:scope</tt> is passed without a <tt>:url</tt> the ;TI"*form just submits to the current URL.;To;;0;	[o;
;	[	I"L<tt>:method</tt> - The method to use when submitting the form, usually ;TI"Jeither "get" or "post". If "patch", "put", "delete", or another verb ;TI"@is used, a hidden input named <tt>_method</tt> is added to ;TI"!simulate the verb over post.;To;;0;	[o;
;	[I"E<tt>:format</tt> - The format of the route the form submits to. ;TI"KUseful when submitting to another resource type, like <tt>:json</tt>. ;TI"*Skipped if a <tt>:url</tt> is passed.;To;;0;	[o;
;	[I"F<tt>:scope</tt> - The scope to prefix input field names with and ;TI"Ethereby how the submitted parameters are grouped in controllers.;To;;0;	[o;
;	[I"M<tt>:namespace</tt> - A namespace for your form to ensure uniqueness of ;TI"Nid attributes on form elements. The namespace attribute will be prefixed ;TI".with underscore on the generated HTML id.;To;;0;	[o;
;	[
I"E<tt>:model</tt> - A model object to infer the <tt>:url</tt> and ;TI";<tt>:scope</tt> by, plus fill out input field values. ;TI"FSo if a +title+ attribute is set to "Ahoy!" then a +title+ input ;TI"%field's value would be "Ahoy!". ;TI"DIf the model is a new record a create form is generated, if an ;TI"<existing record, however, an update form is generated. ;TI"EPass <tt>:scope</tt> or <tt>:url</tt> to override the defaults. ;TI"EE.g. turn <tt>params[:post]</tt> into <tt>params[:article]</tt>.;To;;0;	[o;
;	[I"K<tt>:authenticity_token</tt> - Authenticity token to use in the form. ;TI"IOverride with a custom authenticity token or pass <tt>false</tt> to ;TI"3skip the authenticity token field altogether. ;TI"KUseful when submitting to an external resource like a payment gateway ;TI"(that might limit the valid fields. ;TI"FRemote forms may omit the embedded authenticity token by setting ;TI"S<tt>config.action_view.embed_authenticity_token_in_remote_forms = false</tt>. ;TI"BThis is helpful when fragment-caching the form. Remote forms ;TI"Lget the authenticity token from the <tt>meta</tt> tag, so embedding is ;TI"@unnecessary unless you support browsers without JavaScript.;To;;0;	[o;
;	[	I"J<tt>:local</tt> - By default form submits via typical HTTP requests. ;TI"LEnable remote and unobtrusive XHRs submits with <tt>local: false</tt>. ;TI"7Remote forms may be enabled by default by setting ;TI"I<tt>config.action_view.form_with_generates_remote_forms = true</tt>.;To;;0;	[o;
;	[I"N<tt>:skip_enforcing_utf8</tt> - If set to true, a hidden input with name ;TI"utf8 is not output.;To;;0;	[o;
;	[I"D<tt>:builder</tt> - Override the object used to build the form.;To;;0;	[o;
;	[I"/<tt>:id</tt> - Optional HTML id attribute.;To;;0;	[o;
;	[I"5<tt>:class</tt> - Optional HTML class attribute.;To;;0;	[o;
;	[I"4<tt>:data</tt> - Optional HTML data attributes.;To;;0;	[o;
;	[I"F<tt>:html</tt> - Other optional HTML attributes for the form tag.;T@S;;i;I"
Examples;T@o;
;	[I"NWhen not passing a block, +form_with+ just generates an opening form tag.;T@o;;	[	I";<%= form_with(model: @post, url: super_posts_path) %>
;TI"5<%= form_with(model: @post, scope: :article) %>
;TI"3<%= form_with(model: @post, format: :json) %>
;TI"U<%= form_with(model: @post, authenticity_token: false) %> # Disables the token.
;T;
0o;
;	[I"2For namespaced routes, like +admin_post_url+:;T@o;;	[I":<%= form_with(model: [ :admin, @post ]) do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0o;
;	[I"VIf your resource has associations defined, for example, you want to add comments ;TI"=to the document given that the routes are set correctly:;T@o;;	[I"C<%= form_with(model: [ @document, Comment.new ]) do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0o;
;	[I";Where <tt>@document = Document.find(params[:id])</tt>.;T@S;;i;I"#Mixing with other form helpers;T@o;
;	[I"JWhile +form_with+ uses a FormBuilder object it's possible to mix and ;TI":match the stand-alone FormHelper methods and methods ;TI"from FormTagHelper:;T@o;;	[I"/<%= form_with scope: :person do |form| %>
;TI"*  <%= form.text_field :first_name %>
;TI")  <%= form.text_field :last_name %>
;TI"
;TI",  <%= text_area :person, :biography %>
;TI"I  <%= check_box_tag "person[admin]", "1", @person.company.admin? %>
;TI"
;TI"  <%= form.submit %>
;TI"<% end %>
;T;
0o;
;	[I"LSame goes for the methods in FormOptionsHelper and DateHelper designed ;TI",to work with an object as a base, like ;TI"HFormOptionsHelper#collection_select and DateHelper#datetime_select.;T@S;;i;I"Setting the method;T@o;
;	[I"JYou can force the form to use the full array of HTTP verbs by setting;T@o;;	[I".method: (:get|:post|:patch|:put|:delete)
;T;
0o;
;	[I"Min the options hash. If the verb is not GET or POST, which are natively ;TI"Nsupported by HTML forms, the form will be set to POST and a hidden input ;TI"Mcalled _method will carry the intended verb for the server to interpret.;T@S;;i;I"Setting HTML options;T@o;
;	[I"KYou can set data attributes directly in a data hash, but HTML options ;TI"9besides id and class must be wrapped in an HTML key:;T@o;;	[I"d<%= form_with(model: @post, data: { behavior: "autosave" }, html: { name: "go" }) do |form| %>
;TI"  ...
;TI"<% end %>
;T;
0o;
;	[I"generates;T@o;;	[	I"Q<form action="/posts/123" method="post" data-behavior="autosave" name="go">
;TI"<  <input name="_method" type="hidden" value="patch" />
;TI"  ...
;TI"
</form>
;T;
0S;;i;I"Removing hidden model id's;T@o;
;	[	I"_The +form_with+ method automatically includes the model id as a hidden field in the form. ;TI"^This is used to maintain the correlation between the form data and its associated model. ;TI"ZSome ORM systems do not use IDs on nested models so in this case you want to be able ;TI"to disable the hidden id.;T@o;
;	[I"eIn the following example the Post model has many Comments stored within it in a NoSQL database, ;TI"/thus there is no primary key for comments.;T@o;;	[
I".<%= form_with(model: @post) do |form| %>
;TI"@  <%= form.fields(:comments, skip_id: true) do |fields| %>
;TI"
    ...
;TI"  <% end %>
;TI"<% end %>
;T;
0S;;i;I"Customized form builders;T@o;
;	[	I"MYou can also build forms using a customized FormBuilder class. Subclass ;TI"IFormBuilder and override or define some more helpers, then use your ;TI"Acustom builder. For example, let's say you made a helper to ;TI"-automatically add labels to form inputs.;T@o;;	[I"i<%= form_with model: @person, url: { action: "create" }, builder: LabellingFormBuilder do |form| %>
;TI"*  <%= form.text_field :first_name %>
;TI")  <%= form.text_field :last_name %>
;TI"(  <%= form.text_area :biography %>
;TI"$  <%= form.check_box :admin %>
;TI"  <%= form.submit %>
;TI"<% end %>
;T;
0o;
;	[I"In this case, if you use:;T@o;;	[I"<%= render form %>
;T;
0o;
;	[I"LThe rendered template is <tt>people/_labelling_form</tt> and the local ;TI"5variable referencing the form builder is called ;TI"<tt>labelling_form</tt>.;T@o;
;	[I"KThe custom FormBuilder class is automatically merged with the options ;TI";of a nested +fields+ call, unless it's explicitly set.;T@o;
;	[I"MIn many cases you will want to wrap the above in another helper, so you ;TI"+could do something like the following:;T@o;;	[I"/def labelled_form_with(**options, &block)
;TI"I  form_with(**options.merge(builder: LabellingFormBuilder), &block)
;TI"end;T;
0:
@fileI"+lib/action_view/helpers/form_helper.rb;T:0@omit_headings_from_table_of_contents_below000[I"G(model: nil, scope: nil, url: nil, format: nil, **options, &block);T@jFI"FormHelper;TcRDoc::NormalModule00