我終於能夠爲我的高級搜索提供某種結構。這個想法是有一個從用戶模型中提取數據的搜索。所以我建立了一個搜索模型,這將是一種圍繞用戶模型的包裝。例如:當我說Search.search_for(選項)時,在search_for方法中,它將使用選項在用戶模型上搜索並返回結果,您可以在頁面上顯示結果。我想到了這種方法,因爲我被告知必須複製這些值,但我想我只需要使Search調用底層(已經存在的)用戶模型。設置搜索模式以環繞用戶模型
所以我有一個用戶可以從哪裏搜索的視圖。我必須收集他們想要搜索的所有選項(性別,年齡,郵政編碼,他們是否有孩子,宗教和種族)。收集選項並將表單提交給SearchController。
我有這個概念,但是因爲我是Rails新手,所以我一直在努力執行。下面的代碼實質上就是我所擁有的(減去用戶模型,因爲它充滿了來自應用程序的其他部分)。我不知道如何完成剩下的編碼來解決這個問題。
Searches_controller:
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)
您是否收到任何錯誤?哪部分不工作?請將這些信息添加到您的問題中。 – 2013-04-29 13:52:35
就是這樣。我不知道我在上面做了什麼之後要去哪裏。就我所能得到的那樣,沒有錯誤發生,因爲我還沒有達到那個階段。我是新手,需要相當長的時間來制定這個比賽計劃。所以我需要幫助完成它,所以我可以學習。 – pwz2000 2013-04-29 13:54:58
在'find_users'的末尾添加'users'來返回數組,這可能會修復它。另外,如果你在你的show動作中定義了'@ users',那麼就使用'<%= render @users%>',如果你有'/ users/_user.html.erb'部分,這將會工作。要查看搜索是否已經返回任何東西只是將其轉儲到顯示視圖'<%= @users %>' – 2013-05-02 23:47:17