我正在为我的帖子搜索创建一个表单。我正在这样做......
erb表格代码...
<%= form_tag '/posts/search-post', :remote=> "true" do %>
<p>
<%= text_field_tag :search, params[:search], :placeholder => "Search Posts..." %><br/>
<%= radio_button_tag :day, 1, params[:day] %>None
<%= radio_button_tag :day, 2, params[:day] %>Last Week
<%= radio_button_tag :day, 3, params[:day] %>Last Month<br/>
<%= submit_tag "Search", :onclick => "document.getElementById('spinner').style.visibility='visible';document.getElementById('postlist').style.visibility='hidden'" %>
</p>
<% end %>
根.rb
match 'posts/search-post', to: 'posts#search_post'
post_controller.rb
def search_post
if !params[:search].blank? && params[:day].blank?
@posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search])
elsif params[:search].blank? && !params[:day].blank?
@posts = Post.paginate(page: params[:page],:per_page => 5).all if params[:day] == "1"
@posts = Post.paginate(page: params[:page],:per_page => 5).where("created_at >= ?", 1.week.ago.utc) if params[:day] == "2"
@posts = Post.paginate(page: params[:page],:per_page => 5).where("created_at >= ?", 1.month.ago.utc) if params[:day] == "3"
elsif !params[:search].blank? && !params[:day].blank?
@posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search]) if params[:day] == "1"
@posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search]).where("created_at >= ?", 1.week.ago.utc) if params[:day] == "2"
@posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search]).where("created_at >= ?", 1.month.ago.utc) if params[:day] == "3"
else
end
end
Post.rb 模型
def self.search(search)
search_condition = "%" + search + "%"
if search
find(:all, :conditions => ['lower(content) LIKE ? OR lower(title) LIKE ?', search_condition.downcase,search_condition.downcase])
else
find(:all)
end
end
搜索-post.js.erb
$("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
当我同时按关键字和日期类型搜索时,搜索不起作用(获取所有帖子列表项)。我不知道我错在哪里。请帮忙。