利用ransack實(shí)現(xiàn)站內(nèi)搜索

step 1 add_column

首先rails g migration add_category_to_job
在新增的db/migrate/2017xxxx_create_category_to_job.rb文件中添加

def change
  add_column :jobs, :category_name, :string, default: "工程師"
end```

然后`rake db:migrate`

###step 2  GEM
在Gemfile中添加 

gem 'ransack'
gem 'paginate'
gem 'paginate-bootstrap


 然后 `bundle install`;
  
###step 3  搜索框
  在Navbar增加一個(gè)搜索框

<%= form_tag search_jobs_path, :method => :get, class: "navbar-form navbar-right" do %>
<div class="input-group">
<input type="text" class="form-control" name="q" placeholder="搜索職位、公司" value="<%= params[:q] %>">
<div class="input-group-btn">
<button class="btn btn-default" type="submit">
<i class="fa fa-search" aria-hidden="true"></i>
</button>
</div>
</div>
<% end %>

    
###step 4  控制器
  在jobs_controller中增加相應(yīng)的函數(shù)

def search
if @query_string.present?
search_result = Job.ransack(@search_criteria).result(distinct: true)
@jobs = search_result.paginate(:page => params[:page], per_page: 10 )
puts @jobs
else
@jobs = Job.publish.recent.paginate(page: params[:page], per_page: 10)
puts @jobs
end
end

  
  在`private`下面添加

def validate_search_key
@query_string = params[:q].gsub(/\|'|/|?/, "") if params[:q].present?
@search_criteria = search_criteria(@query_string)
end

def search_criteria(query_string)
{ :title_or_description_or_contact_email_or_location_or_company_or_category_cont => query_string }
end


#注意: 這里搜索是會(huì)報(bào)錯(cuò),是因?yàn)闆](méi)有對(duì)應(yīng)的view顯示搜索結(jié)果(圖中標(biāo)注文字可以忽略)![](http://upload-images.jianshu.io/upload_images/2471238-3797d9e47da1d683.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

###step 5 VIEW 
  生成搜索結(jié)果所需要的對(duì)應(yīng)頁(yè)面 `touch app/views/jobs/search_result.html.erb`
  

<% if @jobs.length > 0 %>
<table class="table table-hover table-boldered table-striped custab">
<thead>
<tr>
<th>職位名稱</th>
<th>工作類型</th>
<th>薪金</th>
<th>公司名稱</th>
<th>公司地點(diǎn)</th>
<th>發(fā)布時(shí)間</th>
</tr>
</thead>
<tbody>
<% @jobs.each do |job| %>
<tr>
<td> <%= link_to(job.title, job_path(job)) %></td>
<td><%= job.category %></td>
<td> <span class="label label-success"><%= job.wage_lower_bound %>K - <%= job.wage_upper_bound %>K</span></td>
<td><%= link_to(job.company, '/jobs/search?utf8=?&q=' + job.company) %></td>
<td><%= job.location %></td>
<td><%= job.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h4 class="text-center">沒(méi)有相關(guān)職位,嘗試搜索其他的關(guān)鍵字</h4>
<div class="text-center">
<a class="btn btn-lg btn-info" href="/jobs">瀏覽職位</a>
</div>
<% end %>```

重新搜索試試看???

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文章分類 后臺(tái)文章分類列表頁(yè)模板導(dǎo)的詳細(xì)步驟建立數(shù)據(jù)表blog_category,并添加相應(yīng)的文章字段使用php ...
    JoyceZhao閱讀 1,863評(píng)論 0 14
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,322評(píng)論 0 17
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,048評(píng)論 0 23
  • 可送出簡(jiǎn)歷:/jobs/1/resumes/new 簡(jiǎn)歷內(nèi)容不為空 使用者得登入才能提交簡(jiǎn)歷 使用attachme...
    Dragon168閱讀 328評(píng)論 0 0
  • 昨晚查了教師資格證的考試地點(diǎn),是在長(zhǎng)沙星沙考 ,學(xué)校在岳麓區(qū)。坐車公交要兩個(gè)小時(shí)。早上十點(diǎn)考試,讓我想起藝考的時(shí)候...
    一理子閱讀 399評(píng)論 0 1

友情鏈接更多精彩內(nèi)容