ここでselect_tagを使用して漢字の単語リストをフィルタリングしようとしています。 ここでは、ドロップダウンからオプションを選択 - >自動提出フォーム - > AJAXを使用してsearch_by_lesson
アクションを取得し、#kanji_listにデータを入力するという考えがあります。私は私のform_tag
でremote: true
を持っている間、私は、ドロップダウンリストから選択すると、ブラウザはまだhttp://localhost:3000/search_kanjis_by_lesson?lesson=1
ではなく、滞在インデックスページで新しいページに移動し、"このリクエスト形式とバリアントのテンプレートがありません。" AJAX応答をレンダリングしようとしたとき
- :しかし、私は2の問題を得ました。
- タイトルの誤り。この
search_by_lesson
はAJAXでレンダリングするだけですが、別のフォルダにはsearch_by_lesson.html.erb
を置いておきたいと思います。
ここで私は私が私のルート
resources :kanjis
get '/search_kanjis_by_lesson', to: 'kanjis#search_by_lesson', as: :search_kanjis_by_lesson
、私のkanjis_controllers中にこれを得たリスト_index.html.erb
<% @kanjis.each do |kanji| %>
<tr>
<td><%= kanji.word %></td>
<td><%= kanji.meaning %></td>
<td><%= kanji.lesson %></td>
</tr>
<% end %>
をレンダリングする部分を使用して、私のindex.html.erb
<%= form_tag search_kanjis_by_lesson_path(lesson: params[:lesson]), method: :get, remote: true do |f| %>
<%= select_tag :lesson, options_for_select(@lessons), include_blank: true, onchange: "this.form.submit()" %>
<% end %>
<table class="ui celled padded striped table">
<thead>
<tr>
<th>Kanji</th>
<th>Meaning</th>
<th>Lesson</th>
</tr>
</thead>
<tbody class="kanji_list">
<%= render 'index' %>
</tbody>
</table>
です。 rb
class KanjisController < ApplicationController
# ... other generic actions: edit, show ...
def index
@kanjis = Kanji.all
@lessons = [*1..32] # @lessons is just an array of int.
end
def search_by_lesson
@kanjis = Kanji.find_by_lesson params[:lesson]
end
end
最後に、私はこれはあなたのアプリケーションで
あなたindex.html.erbで<%= form_tag search_kanjis_by_lesson_path, method: :get, remote: true do %>
<%= select_tag :lesson, options_for_select(@lessons), include_blank: true %>
<% end %>
のために働く必要がありsearch_by_lesson.js.erb
$("#kanji-list").html("<%= escape_javascript(render 'index') %>")
あなたのコントローラ –