前提・実現したいこと
ajax + kaminari でもっと見るボタンを実装したいです。
現在railsで都道府県・市町村の登録機能を実装しているのですが、データ量が多く市町村の一部だけを表示させて、後は「もっと見る」ボタンをクリックさせて表示させようとしています。
今回参考にさせている記事リンク内容によるとgem kaminari
により予め表示件数を指定し、そのgemのメソッドであるlink_to_next_page
を使用し、次の件数を表示する際にremote true
によりjs.erbファイルを読み込むという流れになっています。
今のところ下の画像のように表示は出来ているのですが、もっと見るボタンをクリックしても何も反応がない状態です。
もっと見る
をクリックする事でターミナル画面では処理が走っている事は確認できているのですが、js.erbファイルが読み込まれていないように思えます。
何か以下の記述でおかしい箇所などあるかご教授頂けないでしょうか汗
お忙しいところ申し訳ありません。
該当のソースコード
city.rb class City < ApplicationRecord belongs_to :prefecture has_many :masseurs, through: :business_trip_ranges has_many :business_trip_ranges paginates_per 10 end
business_trip_ranges_controller def edit @prefectures = Prefecture.all @ranges = @current_masseur.business_trip_ranges @cities = City.all.page(params[:page]) end
render partial:
で呼び出す部分をテンプレート化
_cities.html.erb <%= f.collection_check_boxes :city_ids, @cities, :id, :name, include_hidden: false do |c| %> <ul class="city-check-box"> <% if p.object.id == c.object.prefecture_id %> <div id="boxes"> <li class="city-check-box-item"><%= c.check_box + c.text %></li> </div> <% end %> </ul> <% end %>
先ほどの_cities.html.erb
をedit.html.erb
に書き込む。
edit.html.erb <%= form_with(model: @current_masseur, url: store_manager_business_trip_ranges_update_path(@current_masseur), method: :patch, local: true) do |f| %> <table class="prefecture table table-hover"> <thead> <%= collection_check_boxes :prefecture, :prefecture_ids, Prefecture.all, :id, :name, include_hidden: false do |p| %> <tr> <th> <%= p.check_box + p.text %> </th> </tr> </thead> <tbody> <tr> <td class="city-check-boxes"> 質問の部分 → <div id="cities"><%= render partial: 'cities', locals: { f: f, p: p } %></div> 質問の部分 → <div class="more-city-link"><%= link_to_next_page @cities, 'もっと見る', remote: true, id: 'more-cities' %></div> </td> </tr> <% end %> </tbody> </table> <div class="actions text-center"> <h1 id="result">結果</h1> <%= f.submit "登録する", class: "btn btn-primary mt-5 w-50" %> </div> <% end %>
もっと見るボタンをクリックする事でedit.js.erb
が読み込まれる。
edit.js.erb $('#cities').append("<%= escape_javascript(render 'cities', object: @cities) %>"); $("#more-cities").replaceWith("<%= escape_javascript( link_to_next_page(@cities, 'もっと見る', remote: true, id: 'more-cities')) %>");
補足情報(FW/ツールのバージョンなど)
ボタンを押した直後のターミナル画面。コンソールには何も表示されていませんでした。汗
Started GET "/store_manager/masseurs/1/business_trip_ranges/edit.1?page=2" for 49.98.64.183 at 2020-07-24 06:51:57 +0000 Cannot render console from 49.98.64.183! Allowed networks: 127.0.0.0/127.255.255.255, ::1 Processing by StoreManager::BusinessTripRangesController#edit as Parameters: {"page"=>"2", "masseur_id"=>"1"} Masseur Load (0.1ms) SELECT "masseurs".* FROM "masseurs" WHERE "masseurs"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/store_manager/business_trip_ranges_controller.rb:50:in `set_masseur' Rendering store_manager/business_trip_ranges/edit.html.erb within layouts/store_manager Prefecture Load (0.2ms) SELECT "prefectures".* FROM "prefectures" ↳ app/views/store_manager/business_trip_ranges/edit.html.erb:13 City Load (0.1ms) SELECT "cities".* FROM "cities" LIMIT ? OFFSET ? [["LIMIT", 10], ["OFFSET", 10]] ↳ app/views/store_manager/business_trip_ranges/_cities.html.erb:1 Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 1.9ms | Allocations: 1005) (0.1ms) SELECT COUNT(*) FROM "cities" ↳ app/views/store_manager/business_trip_ranges/edit.html.erb:25 Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) (0.1ms) SELECT "cities"."id" FROM "cities" INNER JOIN "business_trip_ranges" ON "cities"."id" = "business_trip_ranges"."city_id" WHERE "business_trip_ranges"."masseur_id" = ? [["masseur_id", 1]] ↳ app/views/store_manager/business_trip_ranges/_cities.html.erb:5 Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 2.8ms | Allocations: 1643) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/_cities.html.erb (Duration: 0.2ms | Allocations: 186) Rendered store_manager/business_trip_ranges/edit.html.erb within layouts/store_manager (Duration: 54.3ms | Allocations: 30370) Rendering layouts/application.html.erb Rendered layouts/application.html.erb (Duration: 0.4ms | Allocations: 193) Completed 200 OK in 67ms (Views: 64.6ms | ActiveRecord: 0.6ms | Allocations: 41715)
回答1件
あなたの回答
tips
プレビュー