如何将过滤器表单与搜索表单链接?

时间:2019-08-18 17:41:17

标签: ruby-on-rails ruby

我有一个用于进行文本搜索的搜索表单和一个用于过滤用户个人资料属性的过滤器表单。一切正常,但是如果我更新过滤器表单,则必须在搜索表单中重新输入文本,该文本将变为空白。如何使过滤器位于当前搜索参数上?我正在使用黑子宝石进行搜索。

这是我的页面控制器。

class PagesController < ApplicationController
    def home
        if current_user.profile
            @search = Chatroom.search do
                fulltext params[:search]
            end
            @chatrooms = @search.results
            @chatrooms.each do |x|
                if x.chatroom_users.count==0
                    @chatrooms.delete(x)
                    next
                end
                @member = x.chatroom_users.first
                @adminuser = User.find(@member.user_id)
                if @adminuser.profile.city != current_user.profile.city
                    @chatrooms.delete(x)
                end
                if x.minimumage > current_user.profile.age || x.maximumage < current_user.profile.age
                    @chatrooms.delete(x)
                end
                if current_user.profile.minimumage > x.minimumage || current_user.profile.maximumage < x.maximumage
                    @chatrooms.delete(x)
                end      
            end
        end 
    end
end

这是我对搜索和过滤器表单以及搜索结果的看法

<%= form_tag root_path(user_id: current_user.id), :method => :get do %>
    <p>
    <%= text_field_tag :search, params[:search], autocomplete: "off", placeholder: "Try \"Tennis\"", class: 'searchbar' %>
    <button type="submit"><i class="fa fa-search"></i>
<% end %>
<%= form_for current_user.profile, url: user_profile_path(user_id: current_user.id) do |f| %>
    <div class="navy">Gender<br></div>
    <%= f.select :genderpreference, ['Only Male','Only Female', 'Must Include Male', 'Must Include Female', 'Does Not Matter'], class: 'input-1' %>
    <% if !current_user.profile.collegeemail.blank? %>
    <div class="navy">College</div>
    <%= f.select :collegepreference, ['Only Students At My College','Only College Students','Must Include Students At My College','Must Include College Students','Does Not Matter'], class: "input-1"%>
    <%end %>
    <div class="navy">Minimum Age</div>
    <%= f.select :minimumage, [13, 14, 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100], class: 'input-1' %>
    <div class="navy">Maximum Age</div>
    <%= f.select :maximumage, [13, 14, 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100], class: 'input-1' %>
    <%= f.submit 'Submit', class: 'btn btn-default', class: 'bu' %>   

<% end %>


这是我的个人资料控制器中的更新功能,它重定向到显示搜索结果的根路径。

def update
    @user = User.find( params[:user_id] )
    @profile = @user.profile
    if @profile.update_attributes(profile_params)
      flash[:success] = "Profile Updated."
      #redirect to their profile page
      redirect_to root_path(user_id: current_user.id), :method => :get
    else
      render action: :edit
    end
  end

0 个答案:

没有答案