ruby-on-rails - 在视图中呈现结果不正确的 ActiveRecord
问题描述
Rails 版本 6,postgresql v 11,ruby 2.7.0
使用 ActiveRecord 根据表单中包含的搜索字段填充视图中的 HTML 表时,我遇到了奇怪的行为。
如果我请求基本 URL
localhost:3000/instui/search
我得到的结果包含(正确)数据库中的前 20 条记录(来自 NYSE 和 NASDAQ 的证券):
如果我在搜索字段中输入 BB,我可以看到“where”已经完成了它的工作,并且刚刚从 puts 中检索了相应的记录到控制台:
Processing by InstuiController#search as JS
Parameters: {"utf8"=>"✓", "q"=>"BB", "commit"=>"Search"}
{"utf8"=>"✓", "commit"=>"Search", "q"=>"BB"}
Instrument Load (1.1ms) SELECT "instruments".* FROM "instruments" WHERE (ticker LIKE '%BB%') LIMIT $1 [["LIMIT", 20]]
↳ app/controllers/instui_controller.rb:19:in `search'
Asia Broadband Inc
ABB Ltd
Auburn Bancorp Inc
AbraPlata Resource Corp
AB DISCOVERY VALUE FUND CLASS B
AbbVie Inc
Abby Inc
AB GROWTH FUND CLASS B
Ambu A/S
Mercantil Bank Holding Corporation
BAB Inc
BlackBerry Limited
New York Health Care Inc
NORTHERN GLOBAL TACTICAL ASSET ALLOCATION FUND NORTHERN GLOBAL TACTICAL ASSET ALLOCATION FUND
BBA Aviation plc
BBA Aviation plc
JPMorgan BetaBuilders Dev Asia ex-JpnETF
BBH LIMITED DURATION FUND CLASS I SHARES
Baker Boyer Bancorp
BlueBird Battery Metals Inc
20
Rendering instui/search.html.erb within layouts/application
Rendered instui/search.html.erb within layouts/application (Duration: 0.4ms | Allocations: 339)
Completed 200 OK in 15ms (Views: 11.8ms | ActiveRecord: 1.1ms | Allocations: 11355)
然而,视图继续呈现与先前请求相同的结果——即所有记录,就好像视图没有从控制器接收到新的实例变量一样。我看不出哪里出错了。帮助表示赞赏。
视图是:
<h2>Search</h2>
<%= form_with(url: "search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<table class="table table-striped">
<tr><th>ID</th><th>Name</th><th>Country</th><th>Ticker</th>
<% @search_results.each do |i| %>
<tr>
<td><%= i.id %></td>
<td><%= i.name %></td>
<td><%= i.country %></td>
<td><%= i.ticker %></td>
</tr>
<% end %>
</table>
控制器是:
class InstuiController < ApplicationController
def index
end
def search
@searchTerms = params.permit(:utf8, :commit, :q)
puts(@searchTerms)
query = @searchTerms[:q]
if query == nil
query = "%%"
end
@search_results = Instrument.where("ticker LIKE ?", "%#{query}%").limit(20)
@search_results.each { |i| puts(i.name) }
puts @search_results.length
end
end
路线:
Rails.application.routes.draw do
get 'test/index'
get 'instui/index'
get 'instui/search'
get 'assetmaster/csvlist'
resources :instruments
get 'instruments/csvlist'
get 'prices/priceservice'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
解决方案
好的,问题是我没有local: true
在表格上设置。表格改写为:
<%= form_with(url: "search", method: "get", local: true) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
问题消失了。
推荐阅读
- javascript - 如何在 Oracle Apex 5 中使用 javascript 从表格中获取选定的行(及其值)
- ssl - ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1108)
- facebook - 从帖子中查找每个反应的计数
- sql - 嵌套 (?) SQL 查询
- ios - URLSession downloadTask(with:) 创建多个文件下载
- javascript - Javascript InnerHtml 来自
- reactjs - 无法将反应应用程序部署到谷歌应用程序引擎
- angular - 只为包输出和运行测试构建一次 Angular 9 项目
- javascript - 在 Angular 中打开新项目时折叠已经消耗的项目
- google-calendar-api - Google 日历 API 将会议数据返回为待处理