diff options
author | Robin Dupret <robin.dupret@gmail.com> | 2017-04-27 14:50:27 +0200 |
---|---|---|
committer | Robin Dupret <robin.dupret@gmail.com> | 2017-04-27 14:50:27 +0200 |
commit | ef291d997d8751e9289cfc148b24d0b9aea81dc8 (patch) | |
tree | d33d399fb93fb534a16d475a43a1465c5c32e70e /guides/source | |
parent | 5974334aeb23c21100af5bf877b385dbbe762bc6 (diff) | |
download | rails-ef291d997d8751e9289cfc148b24d0b9aea81dc8.tar.gz rails-ef291d997d8751e9289cfc148b24d0b9aea81dc8.tar.bz2 rails-ef291d997d8751e9289cfc148b24d0b9aea81dc8.zip |
Update the JavaScript guide to use `form_with`
`form_with` is the new preferred method to deal with generation of
HTML forms and it enables Ajax support by default.
[ci skip]
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/working_with_javascript_in_rails.md | 36 |
1 files changed, 7 insertions, 29 deletions
diff --git a/guides/source/working_with_javascript_in_rails.md b/guides/source/working_with_javascript_in_rails.md index 2a6a87c232..2ef7e8d15d 100644 --- a/guides/source/working_with_javascript_in_rails.md +++ b/guides/source/working_with_javascript_in_rails.md @@ -153,14 +153,15 @@ Unless you have disabled the Asset Pipeline, provides the JavaScript half, and the regular Ruby view helpers add appropriate tags to your DOM. -### form_for +### form_with -[`form_for`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for) -is a helper that assists with writing forms. `form_for` takes a `:remote` -option. It works like this: +[`form_with`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with) +is a helper that assists with writing forms. By default, `form_with` considers +that your form will be using Ajax ; you can opt-out by specifying the `:local` +option. ```erb -<%= form_for(@article, remote: true) do |f| %> +<%= form_with(model: @article) do |f| %> ... <% end %> ``` @@ -168,7 +169,7 @@ option. It works like this: This will generate the following HTML: ```html -<form accept-charset="UTF-8" action="/articles" class="new_article" data-remote="true" id="new_article" method="post"> +<form action="/articles" method="post" data-remote="true"> ... </form> ``` @@ -191,29 +192,6 @@ $(document).ready -> Obviously, you'll want to be a bit more sophisticated than that, but it's a start. You can see more about the events [in the jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki/ajax). -### form_tag - -[`form_tag`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag) -is very similar to `form_for`. It has a `:remote` option that you can use like -this: - -```erb -<%= form_tag('/articles', remote: true) do %> - ... -<% end %> -``` - -This will generate the following HTML: - -```html -<form accept-charset="UTF-8" action="/articles" data-remote="true" method="post"> - ... -</form> -``` - -Everything else is the same as `form_for`. See its documentation for full -details. - ### link_to [`link_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to) |