我的搜索控制器损坏了,没有错误,并且不会相应显示

时间:2019-03-03 06:46:47

标签: ruby-on-rails search

我在搜索/索引中有这个

<%= form_tag search_index_path, method: :get do %>
  <%= text_field_tag :title, params[:title], placeholder: "title" %>
  <%= text_field_tag :content, params[:content], placeholder: "content" %>
  <%= submit_tag "Search", name: nil %>
<% end %>
<div class="section">
  <div class=content>
    <% @posts.each do |post| %>
      <%= post.title %>
      <%= post.content %>
    <% end %>
  </div>
</div

这是我的搜索控制器:

content_term = params[:content]
title_term = params[:title]

if content_term
  @posts = Post.where('content LIKE ?', "%#{content_term}%")
else
  @posts = Post.all.order("created_at DESC")
end

if title_term
  @posts = Post.where('title LIKE ?', "%#{title_term}%")
else
  @posts = Post.all.order("created_at DESC")
end

if content_term != 0 and title_term != 0
  @posts = Post.search(content_term, title_term)
else
  @posts = Post.all.order("created_at DESC")
end

这两个词都不能单独使用,也不能同时使用。

这是Post.rb中的.search方法:

def self.search(content_term, title_term)
  return scoped unless content_term.present? || title_term.present?
  where(['content LIKE ? AND title LIKE ?', "%#{content_term}%", "%#{title_term}%"]) 
end

它可以很好地加载,因此search_index_path的网址在路由中可以正常工作,但这与:gets和:posts或我似乎不知道的奖励数据类型有关。

如您所见,参数已连接到控制器和视图之间,并通过控制器进入SQL,但不会返回帖子。

服务器日志:

Started GET "/search?utf8=%E2%9C%93&title=&content=ods" for 127.0.0.1 at 2019-03-04 08:10:57 +1100
Processing by SearchController#index as HTML
  Parameters: {"utf8"=>"✓", "title"=>"", "content"=>"ods"}
  User Load (3.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/adenhandasyde/.rvm/gems/ruby-2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
   (0.2ms)  BEGIN
  ↳ /Users/adenhandasyde/.rvm/gems/ruby-2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
  Ahoy::Visit Create (9.0ms)  INSERT INTO "ahoy_visits" ("visit_token", "visitor_token", "user_id", "ip", "user_agent", "referrer", "referring_domain", "landing_page", "browser", "os", "device_type", "started_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["visit_token", "fb09791e-c1a1-4fbf-8ef2-1f97d4690d74"], ["visitor_token", "f460dc00-5027-4b60-8c17-5550bbc8905a"], ["user_id", 1], ["ip", "127.0.0.1"], ["user_agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"], ["referrer", "http://0.0.0.0:3000/search?utf8=%E2%9C%93&title=&content=ods"], ["referring_domain", "0.0.0.0"], ["landing_page", "http://0.0.0.0:3000/search?utf8=%E2%9C%93&title=&content=ods"], ["browser", "Chrome"], ["os", "Mac"], ["device_type", "Desktop"], ["started_at", "2019-03-03 21:10:57.981414"]]
  ↳ /Users/adenhandasyde/.rvm/gems/ruby-2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
   (2.2ms)  COMMIT
  ↳ /Users/adenhandasyde/.rvm/gems/ruby-2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [Ahoy::GeocodeV2Job] [92751e46-31db-4118-93b4-d3a8f54e8df7] Performing Ahoy::GeocodeV2Job (Job ID: 92751e46-31db-4118-93b4-d3a8f54e8df7) from Async(ahoy) with arguments: "fb09791e-c1a1-4fbf-8ef2-1f97d4690d74", "127.0.0.1"
[ActiveJob] Enqueued Ahoy::GeocodeV2Job (Job ID: 92751e46-31db-4118-93b4-d3a8f54e8df7) to Async(ahoy) with arguments: "fb09791e-c1a1-4fbf-8ef2-1f97d4690d74", "127.0.0.1"
[ActiveJob] [Ahoy::GeocodeV2Job] [92751e46-31db-4118-93b4-d3a8f54e8df7] Performed Ahoy::GeocodeV2Job (Job ID: 92751e46-31db-4118-93b4-d3a8f54e8df7) from Async(ahoy) in 5.7ms
  Rendering search/index.html.erb within layouts/application
  Post Load (0.6ms)  SELECT "posts".* FROM "posts" WHERE (content LIKE '%ods%' AND title LIKE '%%')
  ↳ app/views/search/index.html.erb:11
  Rendered search/index.html.erb within layouts/application (10.5ms)
   (0.2ms)  BEGIN
  ↳ app/controllers/application_controller.rb:41
  Ahoy::Event Create (6.9ms)  INSERT INTO "ahoy_events" ("visit_id", "user_id", "name", "properties", "time") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["visit_id", 2], ["user_id", 1], ["name", "Ran Action"], ["properties", "{\"controller\":\"search\",\"action\":\"index\"}"], ["time", "2019-03-03 21:10:58.343325"]]
  ↳ app/controllers/application_controller.rb:41
   (0.3ms)  COMMIT
  ↳ app/controllers/application_controller.rb:41
Completed 200 OK in 407ms (Views: 217.5ms | ActiveRecord: 33.9ms)

我不好,它可以正常工作,显示在页面下方800像素处,我错过了。

0 个答案:

没有答案