2014-03-05 4 views
0

고급 검색 양식이 있으며 사용자 선택을 데이터베이스에 저장하는 방법을 알고 싶습니다. 현재 설정은 수행 된 각 검색을 데이터베이스에 저장하므로 사용자가 웹 사이트에서 검색하는 통계를 볼 수 있습니다.검색 기본 설정을 데이터베이스에 저장하는 방법

내가 가지고있는 질문은 웹 사이트로 돌아올 때 미리로드되도록 사용자 선택을 계정에 저장하거나 저장된 검색 옵션 목록을 만들 수있는 방법입니다. 나는 그들이 웹 사이트를 방문 할 때마다 연령대, 우편 번호 등을 입력 할 필요가 없도록 사용자에게 도움이 될 수 있도록 소셜 네트워킹 응용 프로그램을 보유하고 있습니다.

잘 모르겠습니다.

검색 컨트롤러 :

def new 
    @search = Search.new 
    end 

    def create 
    @search = Search.new(params[:search]) 
    if @search.save 
     redirect_to @search 
    else 
     render 'new' 
    end 
    end 

    def show 
    @search = Search.find(params[:id]) 
    @users = @search.users 
    end 

검색 모델 :

def users 
    @users ||= find_users 
    end 

    private 

    def find_users 
     users = User.order(:id) 
     users = users.where(gender: gender) if gender.present? 
     users = users.where(zip_code: zip_code) if zip_code.present? 
     users = users.where(children: children) if children.present? 
     users = users.where(religion: religion) if religion.present? 
     users = users.where(ethnicity: ethnicity) if ethnicity.present? 

     if min_age.present? && max_age.present? 
     min = [ min_age, max_age ].min 
     max = [ min_age, max_age ].max 
     min_date = Date.today - min.years 
     max_date = Date.today - max.years 
     users = users.where("birthday BETWEEN ? AND ?", max_date, min_date) 
     users 
     end 
     users 
    end 
    end 

검색 양식 :

<%= form_for @search do |f| %> 
<div class="field"> 
    <%= f.label :gender %><br /> 
    <%= f.select :gender, ['male', 'female'], :include_blank => true %> 
</div> 

    <div class="field"> 
    <%= f.label :zip_code %><br /> 
    <%= f.text_field :zip_code %> 
    </div> 
    <div class="field"> 
    <%= f.label :children %><br /> 
    <%= f.select :children, [['Yes, they live with me'], ['I want kids now'], ["I want one someday"], ["Not for me"]], :include_blank => true %> 
    </div> 
    <div class="field"> 
    <%= f.label :religion %><br /> 
    <%= f.select :religion, [["Agnostic"], ["Atheist"], ["Christian"], ["Catholic"], ["Buddhist"], ["Hindu"], ["Jewish"], ["Muslim"], ["Spiritual without affiliation"], ["Other"], ["None"], ["Prefer not to say"]], :include_blank => true %> 
    </div> 
    <div class="field"> 
    <%= f.label :ethnicity %><br /> 
    <%= f.select :ethnicity, [["Asian"], ["Biracial"], ["Indian"], ["Hispanic/Latin"], ["Middle Eastern"], ["Native American"], ["Pacific Islander"], ["White"], ["Other"]], :include_blank => true %> 
    </div> <%= f.select :min_age, (18..75), :include_blank => true %> 
    to 
    <%= f.select :max_age, (18..75), :include_blank => true %> 


    <div class="actions"><%= f.submit "Search" %></div> 
<% end %> 

답변

0

당신이로 검색 PARAMS를 직렬화 JSON, 정렬 화 또는 다른 시리얼을 사용할 수 있습니다 그런 다음 문자열을 쿠키 또는 데이터베이스에 저장하십시오.

0

검색 설정을 session에 저장할 수 있습니다. 단점은 클라이언트가 다른 컴퓨터 또는 브라우저에서 사이트를 방문 할 때 세션 ID가 쿠키에 저장되어 있기 때문에 저장된 검색 설정이 없다는 것입니다.

데이터베이스에 설정을 유지하려면 User 모델에 search_settings 필드를 만들고이 필드에 설정을 serialize하십시오. Postgresql을 사용하는 경우 array 또는 hstore 열 유형을 사용할 수 있습니다.

관련 문제