aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/app/assets/javascripts/README.md
blob: c9282ba76f09b5af411a664e8b456d7b98138954 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Ruby on Rails unobtrusive scripting adapter

This unobtrusive scripting support file is developed for the Ruby on Rails framework, but is not strictly tied to any specific backend. You can drop this into any application to:

- force confirmation dialogs for various actions;
- make non-GET requests from hyperlinks;
- make forms or hyperlinks submit data asynchronously with Ajax;
- have submit buttons become automatically disabled on form submit to prevent double-clicking.

These features are achieved by adding certain [`data` attributes][data] to your HTML markup. In Rails, they are added by the framework's template helpers.

## Optional prerequisites

Note that the `data` attributes this library adds are a feature of HTML5. If you're not targeting HTML5, these attributes may make your HTML to fail [validation][validator]. However, this shouldn't create any issues for web browsers or other user agents.

## Installation

### npm

    npm install @rails/ujs --save

### Yarn

    yarn add @rails/ujs

Ensure that `.yarnclean` does not include `assets` if you use [yarn autoclean](https://yarnpkg.com/lang/en/docs/cli/autoclean/).

## Usage

### Asset pipeline

In a conventional Rails application that uses the asset pipeline, require `rails-ujs` in your `application.js` manifest:

```javascript
//= require rails-ujs
```

### ES2015+

If you're using the Webpacker gem or some other JavaScript bundler, add the following to your main JS file:

```javascript
require("@rails/ujs").start()
```

## How to run tests

Run `bundle exec rake ujs:server` first, and then run the web tests by visiting http://localhost:4567 in your browser.

## License

rails-ujs is released under the [MIT License](MIT-LICENSE).

[data]: https://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-attributes "Embedding custom non-visible data with the data-* attributes"
[validator]: https://validator.w3.org/
[csrf]: https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html