ruby-on-rails - 我的搜索控制器没有错误就坏了,不会相应地显示
问题描述
我在搜索/索引中有这个:
<%= 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 的 url 在路由中工作得很好,但它是关于 :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 像素,我错过了它。
解决方案
推荐阅读
- c++ - C++11 右值引用寻址
- ruby - Ruby:如何从 ruby 每种方法返回单值
- javascript - 如何使用数据库通过 api.ai 提出问题
- r - 在没有两个完整周期数据的情况下检测季节性
- hyperledger-fabric - Hyperledger-Composer:ping 网络管理卡时“访问被拒绝”
- wordpress - 如何从子帖子的自定义字段中获得最低价格
- javascript - 将自定义 JavaScript 添加到 Joomla!模板
- css - 如何使用 css 在 iframe 中缩放图像
- r - 使用一些决策规则加速 for 循环(或远离它)
- python - Python Bokeh基本分组vbar - 简化x轴