Ajaxでテーブルの並び替えを行いたいと思っています。
最初、created_atの最新の日付順で表示させ、プルダウンを選んだら、Ajaxで古い順に並び替えるのが理想です。
しかし、データベースからの取得はうまくいっているものの、値の受け渡しが正常に行われていないのか、表示のさせ方が悪いのか、更新がされません。
コンソールの表示
Rendering memochous/index.js.erb Memochou Load (0.3ms) SELECT "memochous".* FROM "memochous" ORDER BY "memochous"."created_at" ASC Rendered memochous/_table.html.erb (2.6ms) Rendered memochous/index.js.erb (21.7ms) Completed 200 OK in 66ms (Views: 46.3ms | ActiveRecord: 0.3ms)
下記のコントローラのコードをみると if request.post?
の部分に違和感を感じられるかと思いますが、処理を呼び出す最適な方法がまだわからないため、とりあえずpostされたら処理を実行する単純な方法で呼びしています。
controller
def index @memochous = Memochou.order(created_at: :desc) if request.post? @memochous = Memochou.order(created_at: :asc) end end
以下のテキストフィールドの部分 <%= text_field_tag :category %>
は、「postされればなんでもいいや」ということでエンターを押した時にpostされるテキストフィールドにしています。
/views/memochous/index.html.erb
<%= form_tag({controller: :memochous, action: :index}, method: :post, remote: true) do %> <%= text_field_tag :category %> <% end %> <table> <tr><th">メモ</th><th>カテゴリー</th><th>メモ日時</th></tr> <% @memochous.each do |memochou| %> <tr> <td><%= memochou.memotitle %></td> <td><%= memochou.category %></td> <td><%= memochou.created_at %></td> </tr> <% end %> </table>
/views/memochous/_table.html.erb
<tr><th">メモ</th><th>カテゴリー</th><th>メモ日時</th></tr> <% @memochous.each do |memochou| %> <tr> <td><%= memochou.memotitle %></td> <td><%= memochou.category %></td> <td><%= memochou.created_at %></td> </tr> <% end %>
/views/memochous/index.js.erb
$('table').html('<%= (render partial: 'table') %>')
また、
$('table').html('<%= (render partial: 'table', object: @memochous) %>')
としてもダメでした。
プルダウンが理想ですが、基本的なこの処理さえ行えればいいので、プルダウンで行う場合の処理はなくても構いません。
解決策をお願いします。
回答1件
あなたの回答
tips
プレビュー