LoginSignup
22
25

More than 5 years have passed since last update.

Rails4で画面遷移しないajax的な更新フォームを作成する

Posted at

viewにremoteとformatを設定して

=form_for @item, remote: true, format: :json, |
  html: {class: 'item_form'} do |f|
  =f.text_field :name
  =f.submit '保存'

responseのjsonを受け取った際になんかやる処理を追加する

$('.item_form').on 'ajax:success', (e, products) ->
  alert("アイテム情報を保存しました")
$(".item_form").on "ajax:error", (xhr, data, status) ->
  alert("アイテム情報の保存に失敗しました")

controllerはデフォルトのscaffoldとかで生成しておけば勝手にjsonレスポンスの記述が入っているので特にやる事ナシ

便利すぎる・・

22
25
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
22
25