diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 71728f2dc8..07a53546f0 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -213,18 +213,18 @@ module FormHelper # In the examples above, the object to be created or edited was # represented by a symbol passed to +form_for+, and we noted that # a string can also be used equivalently. It is also possible, however, - # to pass a model object itself to +form_for+. For example, if @post + # to pass a model object itself to +form_for+. For example, if @article # is an existing record you wish to edit, you can create the form using # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # ... # <% end %> # # This behaves in almost the same way as outlined previously, with a # couple of small exceptions. First, the prefix used to name the input # elements within the form (hence the key that denotes them in the +params+ - # hash) is actually derived from the object's _class_, e.g. params[:post] - # if the object's class is +Post+. However, this can be overwritten using + # hash) is actually derived from the object's _class_, e.g. params[:article] + # if the object's class is +Article+. However, this can be overwritten using # the :as option, e.g. - # # <%= form_for(@person, as: :client) do |f| %> @@ -236,15 +236,15 @@ module FormHelper # Secondly, the field values shown when the form is initially displayed # are taken from the attributes of the object passed to +form_for+, # regardless of whether the object is an instance - # variable. So, for example, if we had a _local_ variable +post+ + # variable. So, for example, if we had a _local_ variable +article+ # representing an existing record, # - # <%= form_for post do |f| %> + # <%= form_for article do |f| %> # ... # <% end %> # # would produce a form with fields whose initial state reflect the current - # values of the attributes of +post+. + # values of the attributes of +article+. # # === Resource-oriented style # @@ -256,49 +256,49 @@ module FormHelper # in config/routes.rb. In this case \Rails will simply infer the # appropriate URL from the record itself. For example, # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # ... # <% end %> # # is then equivalent to something like: # - # <%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %> + # <%= form_for @article, as: :article, url: article_path(@article), method: :patch, html: { class: "edit_article", id: "edit_article_45" } do |f| %> # ... # <% end %> # # And for a new record # - # <%= form_for(Post.new) do |f| %> + # <%= form_for(Article.new) do |f| %> # ... # <% end %> # # is equivalent to something like: # - # <%= form_for @post, as: :post, url: posts_path, html: { class: "new_post", id: "new_post" } do |f| %> + # <%= form_for @article, as: :article, url: articles_path, html: { class: "new_article", id: "new_article" } do |f| %> # ... # <% end %> # # However you can still overwrite individual conventions, such as: # - # <%= form_for(@post, url: super_posts_path) do |f| %> + # <%= form_for(@article, url: super_articles_path) do |f| %> # ... # <% end %> # # You can omit the action attribute by passing url: false: # - # <%= form_for(@post, url: false) do |f| %> + # <%= form_for(@article, url: false) do |f| %> # ... # <% end %> # # You can also set the answer format, like this: # - # <%= form_for(@post, format: :json) do |f| %> + # <%= form_for(@article, format: :json) do |f| %> # ... # <% end %> # - # For namespaced routes, like +admin_post_url+: + # For namespaced routes, like +admin_article_url+: # - # <%= form_for([:admin, @post]) do |f| %> + # <%= form_for([:admin, @article]) do |f| %> # ... # <% end %> # @@ -334,7 +334,7 @@ module FormHelper # # Example: # - # <%= form_for(@post, remote: true) do |f| %> + # <%= form_for(@article, remote: true) do |f| %> # ... # <% end %> # @@ -350,7 +350,7 @@ module FormHelper # You can set data attributes directly by passing in a data hash, but all other HTML options must be wrapped in # the HTML key. Example: # - # <%= form_for(@post, data: { behavior: "autosave" }, html: { name: "go" }) do |f| %> + # <%= form_for(@article, data: { behavior: "autosave" }, html: { name: "go" }) do |f| %> # ... # <% end %> # @@ -368,12 +368,12 @@ module FormHelper # Some ORM systems do not use IDs on nested models so in this case you want to be able # to disable the hidden id. # - # In the following example the Post model has many Comments stored within it in a NoSQL database, + # In the following example the Article model has many Comments stored within it in a NoSQL database, # thus there is no primary key for comments. # # Example: # - # <%= form_for(@post) do |f| %> + # <%= form_for(@article) do |f| %> # <%= f.fields_for(:comments, include_id: false) do |cf| %> # ... # <% end %> @@ -485,12 +485,12 @@ def apply_form_for_options!(object, options) # :nodoc: # Creates a form tag based on mixing URLs, scopes, or models. # # # Using just a URL: - # <%= form_with url: posts_path do |form| %> + # <%= form_with url: articles_path do |form| %> # <%= form.text_field :title %> # <% end %> # # => - #
- # + # + # #
# # # With an intentionally empty URL: @@ -499,37 +499,36 @@ def apply_form_for_options!(object, options) # :nodoc: # <% end %> # # => #
- # + # #
# # # Adding a scope prefixes the input field names: - # <%= form_with scope: :post, url: posts_path do |form| %> + # <%= form_with scope: :article, url: articles_path do |form| %> # <%= form.text_field :title %> # <% end %> # # => - #
- # + # + # #
# # # Using a model infers both the URL and scope: - # <%= form_with model: Post.new do |form| %> + # <%= form_with model: Article.new do |form| %> # <%= form.text_field :title %> # <% end %> # # => - #
- # + # + # #
# # # An existing model makes an update form and fills out field values: - # <%= form_with model: Post.first do |form| %> + # <%= form_with model: Article.first do |form| %> # <%= form.text_field :title %> # <% end %> # # => - #
- # - # + # + # + # #
- # # # Though the fields don't have to correspond to model attributes: # <%= form_with model: Cat.new do |form| %> # <%= form.text_field :cats_dont_have_gills %> @@ -537,13 +536,13 @@ def apply_form_for_options!(object, options) # :nodoc: # <% end %> # # => #
- # - # + # + # #
# # The parameters in the forms are accessible in controllers according to - # their name nesting. So inputs named +title+ and post[title] are - # accessible as params[:title] and params[:post][:title] + # their name nesting. So inputs named +title+ and article[title] are + # accessible as params[:title] and params[:article][:title] # respectively. # # For ease of comparison the examples above left out the submit button, @@ -559,25 +558,25 @@ def apply_form_for_options!(object, options) # :nodoc: # # So when passing such a model record, \Rails infers the URL and method. # - # <%= form_with model: @post do |form| %> + # <%= form_with model: @article do |form| %> # ... # <% end %> # # is then equivalent to something like: # - # <%= form_with scope: :post, url: post_path(@post), method: :patch do |form| %> + # <%= form_with scope: :article, url: article_path(@article), method: :patch do |form| %> # ... # <% end %> # # And for a new record # - # <%= form_with model: Post.new do |form| %> + # <%= form_with model: Article.new do |form| %> # ... # <% end %> # # is equivalent to something like: # - # <%= form_with scope: :post, url: posts_path do |form| %> + # <%= form_with scope: :article, url: articles_path do |form| %> # ... # <% end %> # @@ -606,7 +605,7 @@ def apply_form_for_options!(object, options) # :nodoc: # If the model is a new record a create form is generated, if an # existing record, however, an update form is generated. # Pass :scope or :url to override the defaults. - # E.g. turn params[:post] into params[:article]. + # E.g. turn params[:article] into params[:blog]. # * :authenticity_token - Authenticity token to use in the form. # Override with a custom authenticity token or pass false to # skip the authenticity token field altogether. @@ -639,14 +638,14 @@ def apply_form_for_options!(object, options) # :nodoc: # # When not passing a block, +form_with+ just generates an opening form tag. # - # <%= form_with(model: @post, url: super_posts_path) %> - # <%= form_with(model: @post, scope: :article) %> - # <%= form_with(model: @post, format: :json) %> - # <%= form_with(model: @post, authenticity_token: false) %> # Disables the token. + # <%= form_with(model: @article, url: super_articles_path) %> + # <%= form_with(model: @article, scope: :blog) %> + # <%= form_with(model: @article, format: :json) %> + # <%= form_with(model: @article, authenticity_token: false) %> # Disables the token. # - # For namespaced routes, like +admin_post_url+: + # For namespaced routes, like +admin_article_url+: # - # <%= form_with(model: [ :admin, @post ]) do |form| %> + # <%= form_with(model: [ :admin, @article ]) do |form| %> # ... # <% end %> # @@ -694,13 +693,13 @@ def apply_form_for_options!(object, options) # :nodoc: # You can set data attributes directly in a data hash, but HTML options # besides id and class must be wrapped in an HTML key: # - # <%= form_with(model: @post, data: { behavior: "autosave" }, html: { name: "go" }) do |form| %> + # <%= form_with(model: @article, data: { behavior: "autosave" }, html: { name: "go" }) do |form| %> # ... # <% end %> # # generates # - #
+ # # # ... #
@@ -712,10 +711,10 @@ def apply_form_for_options!(object, options) # :nodoc: # Some ORM systems do not use IDs on nested models so in this case you want to be able # to disable the hidden id. # - # In the following example the Post model has many Comments stored within it in a NoSQL database, + # In the following example the Article model has many Comments stored within it in a NoSQL database, # thus there is no primary key for comments. # - # <%= form_with(model: @post) do |form| %> + # <%= form_with(model: @article) do |form| %> # <%= form.fields(:comments, skip_id: true) do |fields| %> # ... # <% end %> @@ -1049,7 +1048,7 @@ def fields_for(record_name, record_object = nil, options = {}, &block) # # => # # # Using +fields+ with +form_with+: - # <%= form_with model: @post do |form| %> + # <%= form_with model: @article do |form| %> # <%= form.text_field :title %> # # <%= form.fields :comment do |fields| %> @@ -1097,58 +1096,58 @@ def fields(scope = nil, model: nil, **options, &block) # target labels for radio_button tags (where the value is used in the ID of the input tag). # # ==== Examples - # label(:post, :title) - # # => + # label(:article, :title) + # # => # # You can localize your labels based on model and attribute names. # For example you can define the following in your locale (e.g. en.yml) # # helpers: # label: - # post: + # article: # body: "Write your entire text here" # # Which then will result in # - # label(:post, :body) - # # => + # label(:article, :body) + # # => # # Localization can also be based purely on the translation of the attribute-name # (if you are using ActiveRecord): # # activerecord: # attributes: - # post: + # article: # cost: "Total cost" # # # - # label(:post, :cost) - # # => + # label(:article, :cost) + # # => # - # label(:post, :title, "A short title") - # # => + # label(:article, :title, "A short title") + # # => # - # label(:post, :title, "A short title", class: "title_label") - # # => + # label(:article, :title, "A short title", class: "title_label") + # # => # - # label(:post, :privacy, "Public Post", value: "public") - # # => + # label(:article, :privacy, "Public Article", value: "public") + # # => # - # label(:post, :cost) do |translation| + # label(:article, :cost) do |translation| # content_tag(:span, translation, class: "cost_label") # end - # # => + # # => # - # label(:post, :cost) do |builder| + # label(:article, :cost) do |builder| # content_tag(:span, builder.translation, class: "cost_label") # end - # # => + # # => # - # label(:post, :terms) do + # label(:article, :terms) do # raw('Accept Terms.') # end - # # => + # # => def label(object_name, method, content_or_options = nil, options = nil, &block) Tags::Label.new(object_name, method, self, content_or_options, options).render(&block) end @@ -1159,14 +1158,14 @@ def label(object_name, method, content_or_options = nil, options = nil, &block) # shown. # # ==== Examples - # text_field(:post, :title, size: 20) - # # => + # text_field(:article, :title, size: 20) + # # => # - # text_field(:post, :title, class: "create_input") - # # => + # text_field(:article, :title, class: "create_input") + # # => # - # text_field(:post, :title, maxlength: 30, class: "title_input") - # # => + # text_field(:article, :title, maxlength: 30, class: "title_input") + # # => # # text_field(:session, :user, onchange: "if ($('#session_user').val() === 'admin') { alert('Your login cannot be admin!'); }") # # => @@ -1207,8 +1206,8 @@ def password_field(object_name, method, options = {}) # hidden_field(:signup, :pass_confirm) # # => # - # hidden_field(:post, :tag_list) - # # => + # hidden_field(:article, :tag_list) + # # => # # hidden_field(:user, :token) # # => @@ -1234,14 +1233,14 @@ def hidden_field(object_name, method, options = {}) # file_field(:user, :avatar) # # => # - # file_field(:post, :image, multiple: true) - # # => + # file_field(:article, :image, multiple: true) + # # => # - # file_field(:post, :attached, accept: 'text/html') - # # => + # file_field(:article, :attached, accept: 'text/html') + # # => # - # file_field(:post, :image, accept: 'image/png,image/gif,image/jpeg') - # # => + # file_field(:article, :image, accept: 'image/png,image/gif,image/jpeg') + # # => # # file_field(:attachment, :file, class: 'file_input') # # => @@ -1256,9 +1255,9 @@ def file_field(object_name, method, options = {}) # hash with +options+. # # ==== Examples - # text_area(:post, :body, cols: 20, rows: 40) - # # => # # text_area(:comment, :text, size: "20x30") @@ -1330,10 +1329,10 @@ def text_area(object_name, method, options = {}) # # ==== Examples # - # # Let's say that @post.validated? is 1: - # check_box("post", "validated") - # # => - # # + # # Let's say that @article.validated? is 1: + # check_box("article", "validated") + # # => + # # # # # Let's say that @puppy.gooddog is "no": # check_box("puppy", "gooddog", {}, "yes", "no") @@ -1354,11 +1353,11 @@ def check_box(object_name, method, options = {}, checked_value = "1", unchecked_ # To force the radio button to be checked pass checked: true in the # +options+ hash. You may pass HTML options there as well. # - # # Let's say that @post.category returns "rails": - # radio_button("post", "category", "rails") - # radio_button("post", "category", "java") - # # => - # # + # # Let's say that @article.category returns "rails": + # radio_button("article", "category", "rails") + # radio_button("article", "category", "java") + # # => + # # # # # Let's say that @user.receive_newsletter returns "no": # radio_button("user", "receive_newsletter", "yes") @@ -1739,7 +1738,7 @@ def initialize(object_name, object, template, options) # # return the
element's id attribute. # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # <%# ... %> # # <% content_for :sticky_footer do %> @@ -1761,7 +1760,7 @@ def id # Return the value generated by the FormBuilder for the given # attribute name. # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # <%= f.label :title %> # <%= f.text_field :title, aria: { describedby: f.field_id(:title, :error) } %> # <%= tag.span("is blank", id: f.field_id(:title, :error) %> @@ -1770,7 +1769,7 @@ def id # In the example above, the element built by # the call to FormBuilder#text_field declares an # aria-describedby attribute referencing the - # element, sharing a common id root (post_title, in this + # element, sharing a common id root (article_title, in this # case). def field_id(method, *suffixes, namespace: @options[:namespace], index: @options[:index]) @template.field_id(@object_name, method, *suffixes, namespace: namespace, index: index) @@ -1782,14 +1781,14 @@ def field_id(method, *suffixes, namespace: @options[:namespace], index: @options # Return the value generated by the FormBuilder for the given # attribute name. # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # <%= f.text_field :title, name: f.field_name(:title, :subtitle) %> - # <%# => %> + # <%# => %> # <% end %> # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # <%= f.text_field :tag, name: f.field_name(:tag, multiple: true) %> - # <%# => %> + # <%# => %> # <% end %> # def field_name(method, *methods, multiple: false, index: @options[:index]) @@ -2344,52 +2343,52 @@ def fields(scope = nil, model: nil, **options, &block) # # ==== Examples # label(:title) - # # => + # # => # # You can localize your labels based on model and attribute names. # For example you can define the following in your locale (e.g. en.yml) # # helpers: # label: - # post: + # article: # body: "Write your entire text here" # # Which then will result in # # label(:body) - # # => + # # => # # Localization can also be based purely on the translation of the attribute-name # (if you are using ActiveRecord): # # activerecord: # attributes: - # post: + # article: # cost: "Total cost" # # # # label(:cost) - # # => + # # => # # label(:title, "A short title") - # # => + # # => # # label(:title, "A short title", class: "title_label") - # # => + # # => # - # label(:privacy, "Public Post", value: "public") - # # => + # label(:privacy, "Public Article", value: "public") + # # => # # label(:cost) do |translation| # content_tag(:span, translation, class: "cost_label") # end - # # => + # # => # # label(:cost) do |builder| # content_tag(:span, builder.translation, class: "cost_label") # end - # # => + # # => # # label(:cost) do |builder| # content_tag(:span, builder.translation, class: [ @@ -2397,12 +2396,12 @@ def fields(scope = nil, model: nil, **options, &block) # ("error_label" if builder.object.errors.include?(:cost)) # ]) # end - # # => + # # => # # label(:terms) do # raw('Accept Terms.') # end - # # => + # # => def label(method, text = nil, options = {}, &block) @template.label(@object_name, method, text, objectify_options(options), &block) end @@ -2458,10 +2457,10 @@ def label(method, text = nil, options = {}, &block) # # ==== Examples # - # # Let's say that @post.validated? is 1: + # # Let's say that @article.validated? is 1: # check_box("validated") - # # => - # # + # # => + # # # # # Let's say that @puppy.gooddog is "no": # check_box("gooddog", {}, "yes", "no") @@ -2483,11 +2482,11 @@ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") # To force the radio button to be checked pass checked: true in the # +options+ hash. You may pass HTML options there as well. # - # # Let's say that @post.category returns "rails": + # # Let's say that @article.category returns "rails": # radio_button("category", "rails") # radio_button("category", "java") - # # => - # # + # # => + # # # # # Let's say that @user.receive_newsletter returns "no": # radio_button("receive_newsletter", "yes") @@ -2508,9 +2507,9 @@ def radio_button(method, tag_value, options = {}) # hidden_field(:pass_confirm) # # => # - # # Let's say that @post.tag_list returns "blog, ruby": + # # Let's say that @article.tag_list returns "blog, ruby": # hidden_field(:tag_list) - # # => + # # => # # # Let's say that @user.token returns "abcde": # hidden_field(:token) @@ -2540,17 +2539,17 @@ def hidden_field(method, options = {}) # file_field(:avatar) # # => # - # # Let's say that @post has image: + # # Let's say that @article has image: # file_field(:image, :multiple => true) - # # => + # # => # - # # Let's say that @post has attached: + # # Let's say that @article has attached: # file_field(:attached, accept: 'text/html') - # # => + # # => # - # # Let's say that @post has image: + # # Let's say that @article has image: # file_field(:image, accept: 'image/png,image/gif,image/jpeg') - # # => + # # => # # # Let's say that @attachment has file: # file_field(:file, class: 'file_input') @@ -2563,12 +2562,12 @@ def file_field(method, options = {}) # Add the submit button for the given form. When no value is given, it checks # if the object is a new resource or not to create the proper label: # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # <%= f.submit %> # <% end %> # - # In the example above, if @post is a new record, it will use "Create Post" as - # submit button label; otherwise, it uses "Update Post". + # In the example above, if @article is a new record, it will use "Create Article" as + # submit button label; otherwise, it uses "Update Article". # # Those labels can be customized using I18n under the +helpers.submit+ key and using # %{model} for translation interpolation: @@ -2584,7 +2583,7 @@ def file_field(method, options = {}) # en: # helpers: # submit: - # post: + # article: # create: "Add %{model}" # def submit(value = nil, options = {}) @@ -2596,12 +2595,11 @@ def submit(value = nil, options = {}) # Add the submit button for the given form. When no value is given, it checks # if the object is a new resource or not to create the proper label: # - # <%= form_for @post do |f| %> + # <%= form_for @article do |f| %> # <%= f.button %> # <% end %> - # - # In the example above, if @post is a new record, it will use "Create Post" as - # button label; otherwise, it uses "Update Post". + # In the example above, if @article is a new record, it will use "Create Article" as + # button label; otherwise, it uses "Update Article". # # Those labels can be customized using I18n under the +helpers.submit+ key # (the same as submit helper) and using %{model} for translation interpolation: @@ -2617,15 +2615,15 @@ def submit(value = nil, options = {}) # en: # helpers: # submit: - # post: + # article: # create: "Add %{model}" # # ==== Examples - # button("Create post") - # # => + # button("Create article") + # # => # # button(:draft, value: true) - # # => + # # => # # button do # content_tag(:strong, 'Ask me!') @@ -2638,13 +2636,13 @@ def submit(value = nil, options = {}) # content_tag(:strong, text) # end # # => # # button(:draft, value: true) do # content_tag(:strong, "Save as draft") # end - # # => #