在搜索后选择Box Selected Item

时间:2012-11-06 16:13:35

标签: ruby-on-rails twitter-bootstrap ruby-on-rails-3 drop-down-menu html-table

我目前有一个按几个选择框排序的表格。选择框正常工作并对表进行排序/过滤,但每当用户提交搜索时,页面都会刷新,选择框将重置。我想在搜索后选择项目,例如,如果您选择男性然后点击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>

articles_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

1 个答案:

答案 0 :(得分: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。

希望这会有所帮助,快乐的栏杆!