0

現在、いくつかの選択ボックスでソートされたテーブルがあります。選択ボックスは正しく機能し、テーブルを並べ替え/フィルター処理しますが、ユーザーが検索を送信するたびに、ページが更新され、選択ボックスがリセットされます。選択したアイテムを検索後に固定したいので、たとえば、[男性] を選択して [go] をクリックすると、ページが更新された後も男性が選択されたままになります。私は現在、無限スクロールを正常に実装しており、フレームワークに Rails 3 と twitter-bootstrap を使用しています。

これが私のコードです:

index.html.erb

 <form class="form-inline"
        <p>
         <label class="radio">
          <input type="radio" name="gender_search" value="M">M
         </label>
        <label class="radio">
          <input type="radio" name="gender_search" value="F">F
        </label>

        <select name="state_search" class="span2">
          <option value="">Select a State</option>
          <option>----------------</option>
          <option>LA</option>
          <option>MS</option>
          <option>TX</option>
        </select>
        <select name="city_search" class="span2">
          <option value="">Select a City</option>
          <option>----------------</option>
          <option>Mandeville</option>
          <option>Covington</option>
        </select>
        <button type="submit" class="btn">GO</button>
        </p>
      </form>
      <table class="table table-striped table-bordered span8 table-condensed"     
       id="articles_table" >
        <thead class="header">
          <tr>
            <th>ID</th>
            <th>Title</th>
            <th>Description</th>
            <th>Created_At</th>
          </tr>
        </thead>
        <tbody>
          <%= render @articles %>
        </tbody>

_article.html.erb

<tr>
      <td> <%= article_counter +1 %> </td>
      <td> <%= article.Title %> </td>
      <td> <%= article.Description %> </td>
      <td> <%= article.Created_At %> </td>
</tr>

article_controller.rb

def index
@articles = Article.state_search(params[:state_search]).gender_search(params[:gender_search]).city_search(params[:city_search]).page(params[:page]).limit(50).order('NTRP DESC', 'Last_Name ASC')
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @articles }
    format.js
  end
end
4

1 に答える 1

2

良い、

select_tag(:city_id, options_for_select([['Lisbon', 1], ['Madrid', 2]], @previous_choice)

まず、Railsのフォームヘルパーを使用して、物事をすっきりさせることをお勧めします。フォームは次のようになります。

<%= form_tag articles_path, :method => "GET" do %>

    <%= radio_button_tag :gender_search, "M" %>
    <%= label_tag :gender_search_M, "M" %>
    <%= radio_button_tag :gender_search, "F" %>
    <%= label_tag :gender_search_F, "F" %>

    <%= select_tag :state_search, options_for_select([['Select a state', 0],['-----------', 0],['LA', 'LA'],['MS', 'MS'], ['TX', 'TX']], @prev_state) %>
    <%= select_tag :city_search, mptions_for_select([['Select a city', 0],['----------', 0],['Mandeville', 'Mandeville'],['Covington', 'Covington']], @prev_city) %>

    <%= submit_tag "Go" %>

<% end %>

select_tagの@prev_stateと@prev_cityに注意してください。これらはarticles_controllerで設定されます:

def index

    @prev_city = params[:city_search]
    @prev_state = params[:state_search]

    # other stuff
end

したがって、ユーザーが送信するたびに、params [:xxx]にはユーザーが選択したものがすべて含まれます。次に、ビューで使用できる変数を作成します。これを使用して、options_for_selectにフィードします。

これがお役に立てば幸いです、幸せな手すり!

于 2012-11-06T17:18:18.403 に答える