diff options
author | Zachary Scott <e@zzak.io> | 2015-02-16 09:50:35 -0800 |
---|---|---|
committer | Zachary Scott <e@zzak.io> | 2015-02-16 09:50:35 -0800 |
commit | 3889363fb0997b6b743533daa48218e5f160738b (patch) | |
tree | b7d8fef000b2c1416de17fe3a93f1c519080e62b | |
parent | 7b75551a1a4539876f878f37a2439cd02f89d961 (diff) | |
parent | f8114617bade2062bf7308452921da8df548ee78 (diff) | |
download | rails-3889363fb0997b6b743533daa48218e5f160738b.tar.gz rails-3889363fb0997b6b743533daa48218e5f160738b.tar.bz2 rails-3889363fb0997b6b743533daa48218e5f160738b.zip |
Merge pull request #18958 from andreynering/guides-form-remote
Improving remote forms guides [ci skip]
-rw-r--r-- | guides/source/working_with_javascript_in_rails.md | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/guides/source/working_with_javascript_in_rails.md b/guides/source/working_with_javascript_in_rails.md index f3d3a83afc..e3856a285a 100644 --- a/guides/source/working_with_javascript_in_rails.md +++ b/guides/source/working_with_javascript_in_rails.md @@ -191,6 +191,34 @@ $(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). +Another possibility is returning javascript directly from the server side on +remote calls: + +```ruby +# articles_controller +def create + respond_to do |format| + if @article.save + format.html { ... } + format.js do + render js: <<-endjs + alert('Article saved successfully!'); + window.location = '#{article_path(@article)}'; + endjs + end + else + format.html { ... } + format.js do + render js: "alert('There are empty fields in the form!');" + end + end + end +end +``` + +NOTE: If javascript is disabled in the user browser, `format.html { ... }` +block should be executed as fallback. + ### form_tag [`form_tag`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag) |