0

我终于能够为我的高级搜索提出某种结构。这个想法是进行从用户模型中提取数据的搜索。所以我建立了一个搜索模型,它将成为用户模型的一种包装器。例如:当我说 Search.search_for(options) 时,在 search_for 方法中它将使用选项在 User 模型上搜索并返回结果,您可以将其显示在页面上。我想出了这个方法,因为我被告知我必须复制这些值,但我想我只需要让 Search 调用底层(已经存在的)用户模型。

所以我有一个用户可以搜索的视图。我必须收集他们指定要搜索的所有选项(性别、年龄、邮政编码、他们是否有孩子、宗教和种族)。收集选项并将表单提交给 SearchController。

我有这个概念,但由于我是 Rails 的新手,所以我在执行过程中苦苦挣扎。下面的代码基本上就是我所拥有的(减去用户模型,因为它填充了来自应用程序的其他部分)。我不确定如何完成其​​余的编码以完成此任务。

搜索控制器:

  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 = Users.search(params)
  end
end

搜索型号:

 attr_accessible :age, :children, :ethnicity, :gender, :religion, :zip_code

  def users
    @users ||= find_users
  end

  def self.search(params)

  end

private

def find_users
  users = User.order(:id)
  users = users.where(gender: gender) if gender
  users = users.where(:ethnicity => ethnicity) if ethnicity
end

new.html(高级搜索页面):

<%= form_for @search do |f| %>
<div class="field">
  <%= f.label :gender %><br />
  <%= f.select :gender, ['man', 'woman'], :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>
  <div class="actions"><%= f.submit "Search" %></div>
<% end %>

show.html(查看显示结果):

<%= render @search.users %>

开发日志:

Started POST "/searches" for 127.0.0.1 at 2013-05-06 14:00:54 -0400
Processing by SearchesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Pher65dG6gRU9NGgv2q1ot0cfjq+MELgXE6dOtvcrY0=", "search"=>{"gender"=>"", "zip_code"=>"", "children"=>"", "religion"=>"", "ethnicity"=>"Asian"}, "commit"=>"Search"}
  [1m[36m (0.2ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (107.5ms)[0m  INSERT INTO `searches` (`age`, `children`, `created_at`, `ethnicity`, `gender`, `religion`, `updated_at`, `zip_code`) VALUES (NULL, NULL, '2013-05-06 18:00:54', 0, NULL, NULL, '2013-05-06 18:00:54', '')
  [1m[36m (60.1ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/searches/25
Completed 302 Found in 276ms (ActiveRecord: 167.7ms)


Started GET "/searches/25" for 127.0.0.1 at 2013-05-06 14:00:54 -0400
Processing by SearchesController#show as HTML
  Parameters: {"id"=>"25"}
  [1m[35mSearch Load (0.5ms)[0m  SELECT `searches`.* FROM `searches` WHERE `searches`.`id` = 25 LIMIT 1
  [1m[36mUser Load (73.2ms)[0m  [1mSELECT `users`.* FROM `users` WHERE `users`.`zip_code` = '' AND `users`.`ethnicity` = '0' ORDER BY id[0m
  Rendered collection (0.0ms)
  Rendered searches/show.html.erb within layouts/application (81.4ms)
  [1m[35mUser Load (0.6ms)[0m  SELECT `users`.* FROM `users` WHERE `users`.`auth_token` = 'LTzif2q6921TM4pQzfmEGg' LIMIT 1
Completed 200 OK in 309ms (Views: 224.6ms | ActiveRecord: 74.3ms)
4

1 回答 1

1

好的,首先确保User模型和Search模型具有完全相同的属性,所以gender, zip_code, children, religionethnicity

唯一需要使用不同列名的情况是,如果您想做一些更“复杂”的事情,例如搜索范围,例如如果 users 表有age,那么搜索表可能有min_ageand max_age

搜索控制器:

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 = User.order(:id)
  users = users.where(gender: gender) if gender
  users = users.where(zip_code: zip_code) if zip_code
  users = users.where(children: children) if children
  users = users.where(religion: religion) if religion
  users = users.where(ethnicity: ethnicity) if ethnicity
  users
end

app/views/searches/show.html.erb

<h1>Search results</h1>
<%= render @users %>

并确保你有这样的部分app/views/users/_user.html.erb

<p><%= user.name %> - <%= user.email %></p> # or whatever

现在转到搜索表单并创建一个新的搜索对象,从 URL 中获取 ID(假设它是 34)。然后进入rails console并执行:

@search = Search.find(34)
# should return something like: gender: "male", zip_code: nil, children: "Yes"..

# then try the users method:
@search.users

不知道为什么您的视图不起作用。确保您@users = @search.users在搜索控制器中有show操作,然后转到views/searches/show.html.erb并执行以下操作:

<% @users.each do |user| %>
  <p><%= user.name %></p>
<% end %>

看看这是否有效。

于 2013-04-29T14:12:19.600 回答