0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails,Ajax】$.ajaxのurlについて

Last updated at Posted at 2020-02-07

##やりたいこと
画面に値を入力し、非同期通信でtodos_controllerのcreateメソッドへ送り、登録処理を行う。

##疑問
$.ajax内のurlに「/todos.json」を設定しているが、なぜcreateメソッドが呼び出されるのか?
createへ行くには「/todos」を指定すべき?

$.ajax({
      type: 'POST',
      url: '/todos.json',
      data: {
        todo: {
          content: todo
        }
      },
      dataType: 'json'
    })
```

```
Prefix Verb  URI Pattern          Controller#Action
todos  POST  /todos(.:format)     todos#create
```
##調べてわかったこと
(.:format) によって、「/todos.json」のような拡張子の指定もできる。
よって上記url指定でもcreateメソッドが呼び出される。

ちなみにurlを「/todos」にしても正常に動作しました。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?