我有一个高级搜索表单,我想向用户提供保存选项的选项。我怎么能做到这一点?
用户应该可以填写高级搜索表单的选项,然后单击“保存此搜索”链接,该链接将存储他们的选项。有了这个,每个用户都将拥有自定义的默认搜索选项,因此每次访问时都不需要重新输入详细信息。
我认为这对未来的很多人都有帮助。
搜索控制器:
def new
@search = Search.new
render layout: 'new_application'
end
def create
@search = Search.new(params[:search])
if @search.save
redirect_to @search
else
render 'new'
end
end
def show
@user = current_user
@search = Search.find(params[:id])
@users = @search.users
render 'users/index', layout: 'new_application'
end
def update
@search = Search.find(params[:id])
if @search.update_attributes(params[:search])
redirect_to @search
else
render 'new'
end
end
def index
if location = Location.find_by_zipcode(params[:search])
latitude = location.latitude * Math::PI / 180
longitude = location.longitude * Math::PI / 180
locations = Location.search(
:geo => [latitude, longitude],
:with => {:geodist => 0.0..600_000.0},
:order => 'geodist ASC',
:per_page => 5_000
)
@users = User.where(zip_code: locations.map(&:zipcode))
else
@users = User.search(params[:search])
end
end
def min_age
@min_age = params[:min_age].to_i.years
end
def max_age
@max_age = params[:max_age].to_i.years
end
def youngest_age
@youngest_age = params[:youngest_age].years
end
def oldest_age
@oldest_age = params[:oldest_age].years
end
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
搜索表单:
a.adv_search href="#"
= image_tag "adv_link.png"
span Advanced Search
= form_for @search do |f|
.form_container
.row
.col
label I am a
select.large
option Female
option Male
.select2
.col.col2
label Seeking
= f.select :gender, %w(Female Male)
.select1
.col.col3
label Ages
= f.select :min_age, options_for_select((18..50),25), {}, {class: 'small'}
.select5
.col.col4
label to
= f.select :max_age, options_for_select((20..50),45), {}, {class: 'small'}
.row
.col.col5
label Near
= f.text_field :zip_code,placeholder: "enter zip here", class: 'text_input'
.col.col6
= f.select :children, ['I want kids now','I want one someday'], prompt: 'child preference'
.select4
.col.col6
= f.select :religion, ['Agnostic', 'Atheist', 'Christian', 'Catholic', 'Buddhist', 'Hindu', 'Jewish', 'Muslim', 'Spiritual without affiliation', 'Other', 'None', 'Prefer not to say'], prompt: 'Religion'
.select4
.col.col7
= f.select :ethnicity, ['Asian', 'Biracial', 'Indian', 'Hispanic/Latin', 'Middle Eastern', 'Native American', 'Pacific Islander', 'White', 'Other'], prompt: 'Ethnicity'
.btm_sec
ul.form_list
li
a href="#"
= image_tag "form_icon1.png"
span.color Save this Search
li
a href="#"
= image_tag "form_icon2.png"
span Load
li
a href="#"
= image_tag "form_icon3.png"
span Reset
input.find_btn type="submit" value="Find" /
.btm_search_detail
答案 0 :(得分:2)
如果您不想将它们存储在模型中,最好的方法是存储在序列化哈希中。我已经为一个应用程序做了这个,我需要管理员来保存他们的偏好/设置。我尝试了很多宝石,最后我使用了rails-settings。
即,在您的用户模型中,您可以执行以下操作:
class User < ActiveRecord::Base
has_settings do |s|
s.key :search_options, :defaults => { :gender => 'male'}
end
end
然后设置/保存设置,您可以执行以下操作:
user.settings(:search_options).gender = 'female'
user.save!
我喜欢这种方法的原因是它的占地面积很小,如果你开始看到可扩展性问题,那么很容易将其剥离并实现完整的优化模型。
答案 1 :(得分:1)
<强>持久性强>
我个人希望将您的数据保存在另一个关联模型中,如下所示:
#app/models/user.rb
Class User < ActiveRecord::Base
has_one :search
delegate :input, :advanced, :options, to: :search, prefix: true
end
#app/models/search.rb
Class Search < ActiveRecord::Base
#fields id | user_id | input | other | advanced | options | created_at | updated_at
belongs_to :user
end
这意味着如果您能够通过创建“搜索”选项填充模型,您将能够创建所需的功能
现在,作为一个警告,我不知道你的Search
模型现在做了什么 - 但我会详细说明如果 {em>我将如何模型只是出于我自己的想法:
Search
这使您能够使用特定于用户的高级选项填充表单:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
before_action :set_search
private
def set_search
@search_option = current_user.search_input if user_signed_in?
end
end
如果存在用户
,这将设置默认选项-
<强>模型强>
当然,这意味着您必须为您的用户创建另一个模型/数据库条目。但是,这也意味着您可以在不同的调用中保留选项
您对多个“默认”搜索参数的描述是我不完全理解的 - 但包括处理它的模型将使您能够确保数据保持持久
要填充#app/views/shared/_search.html.erb
<%= form_tag search_path do |f| %>
<%= text_field_tag :search, value: @search_option %>
<%= submit_tag "Search" %>
<% end %>
模型,您必须在创建新查找时调用/创建关联对象:
Search
我想,在查看您的代码之后,一种更简洁的方式来管理搜索对象使用关联:)