#試したこと
ruby
1 <td><%= check_box_tag "words_info[]",word.id,@words_info==word.id,{id:id} %><label for="<%=id%>" >追加</label></td>
@words_info==word.id
このように第三引数でparamsのword.idとword.idを比較してtrue,falseを判定することによって選択状態を維持しようとした。
##view
ruby
1<h2>テストを作成してください</h2><br> 2<h3>検索</h3> 3 4<%= search_form_for @q,url:tests_start_path,role: true do |f| %> 5 <%= f.label :title_cont, 'タイトル' %> 6 <%= f.text_field :title_cont %> 7 <%= f.label :user_name_cont,"作者"%> 8 <%= f.text_field :user_name_cont%> 9 <%= f.label :question_cont,"問題"%> 10 <%= f.text_field :question_cont%> 11 <%= f.label :answer_cont,"答え"%> 12 <%= f.text_field :answer_cont%> 13 <%= f.hidden_field "words_info[]",value:params[:words_info]%> 14 <%= button_tag :type => "submit",:class => "btn btn-send" do%> 15 <i class="material-icons left">search</i>検索 16 <% end %> 17<%end%> 18 19<div class="col-md-12"> 20 <%= render "layouts/tabs_menu"%> 21 <%= form_tag(action: :ready) do %> 22 <%=render "words/words_index"%> 23 24 <p>テストのオプション(通常or練習)</p> 25 <%= select_tag 'test_option', options_for_select(["通常", "練習"]) %><br> 26 <p>テストのモード(選択or入力)</p> 27 <%= select_tag 'test_mode', options_for_select(["選択", "入力"]) %><br> 28 <p>テストのオプション(通常or反転)</p> 29 <%= select_tag 'option', options_for_select(["通常", "反転"])%> 30 <%= submit_tag "テストスタート" , class:"btn btn-large btn-primary"%> 31 <% end%> 32 <p>練習モードの場合、正答が問題文に表示されます</p> 33</div> 34
ruby
1<%= paginate@words %> 2<div class="col-md-12"> 3 <div class="table-responsive"> 4 <table class="table table-hover"> 5 <thead> 6 <tr> 7 <th>タイトル</th> 8 <th>問題</th> 9 <th>回答</th> 10 <th>作成時期</th> 11 <%unless params[:action] == 'start'||params[:controller]=="groups" %> 12 <th>作者</th> 13 <th>お気に入り</th> 14 <%end%> 15 <%if params[:action] == 'start'%> 16 <th>選択</th> 17 <%end%> 18 <%if params[:controller]=="groups"%> 19 <th>選択</th> 20 <%end%> 21 </tr> 22 </thead> 23 <tbody> 24 <%= render @words%> 25 </tbody> 26 </table> 27 </div> 28</div> 29<%= paginate@words %> 30
ruby
1<tr id="words-<%= word.id %>"> 2 <td><%= link_to word.title, word%></td> 3 <td><%= word.question %></td> 4 <td><%= word.answer %></td> 5 <td><%= time_ago_in_words(word.created_at) %> 前 </td> 6 7 <%unless params[:action] == 'start'||params[:controller]=="groups" %> 8 <td> 9 <%= link_to word.user.name, user_path(word.user_id)%> 10 </td> 11 <td> 12 <% if word.favorite?(current_user) %> 13 <%= link_to "Release Favorites", word_favorite_path(word), method: :delete, class:"btn btn-danger" %> 14 <% else %> 15 <%= link_to "Add Favorites", word_favorite_path(word), method: :post, class:"btn btn-success" %> 16 <% end %> 17 </td> 18 <%end%> 19 20 21 <%if params[:action] == 'start'%> 22 <%id=word.id%> 23 <td><%= check_box_tag "words_info[]",word.id,@words_info==word.id,{id:id} %><label for="<%=id%>" >追加</label></td> 24 <%end%> 25 <%if params[:controller]=="groups"%> 26 <td><label >追加<%= check_box_tag "group[word_ids][]",word.id %> </label></td> 27 <%end%> 28</tr> 29 30 31
##controller
ruby
1class TestsController < ApplicationController 2 before_action :tag_cloud 3 #@@result_ary = []の説明 クラス変数、resultメソッドで使うため、クラス変数は、スコープ範囲が大きい 4 5 def start 6 @words_info = params[:words_info] 7 @q=Word.ransack(params[:q]) 8 @words=@q.result.page(params[:page]).per(10) 9 puts params["words_info"] 10 flash[:info] = "気に入った問題をお気に入りにしてforkすれば、テストに追加することができます" 11 end 12end
##環境
- ruby 2.3
- rails 4.2.8
- materialize
あなたの回答
tips
プレビュー