LoginSignup
3
3

More than 5 years have passed since last update.

gem 'nested_form_fields'のremove_nested_fields_linkをRails 5で使うときの注意点

Posted at

gem 'nested_form_fields'のremove_nested_fields_linkをRails 5で使うときの注意点

gem 'nested_form_fields'とは

nested_form_fields はネストされたフォームの作成などを助けるgem

問題:remove_nested_fields_linkが正常に動作しない

ヘルパーメソッドの一つであるremove_nested_fields_linkはネストされたフォームの動的削除のメソッドで以下のように使う(はず)。

new.html.erb
<%= f.nested_fields_for :transitpoints, wrapper_tag: :div do |ft| %>
  ...省略...
  <%= ft.remove_nested_fields_link 'Delete', class: 'btn btn-danger', role: 'button'%>
<% end %>

そして生成されたHTMLのページでDeleteボタンを押すと、追加されたフォームが削除されるはずが、
Uncaught TypeError: Cannot read property 'allowAction' of undefinedが発生してしまう。

解決策

//= require jquery_ujsを追加する

app/assets/javascript/application.js
  ...省略...
  //= require jquery_ujs

なぜか?や本当に?とかは答えられません。
むしろ知っている人がいたら教えてほしいです。

ちなみに私が調べた範囲ではRails5になるにあたり、今までjquery_ujsだったものが、rails_ujsになったとのことでした。
(が、rails_ujsは自分の環境ではrequireしているのにエラーになるのはjquery_ujs内のものがrails_ujsに移動したわけではないからと予想しています)

3
3
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
3
3