gpt4 book ai didi

ruby-on-rails - ActiveRecord 在 View 中呈现时结果不正确

转载 作者:行者123 更新时间:2023-12-04 10:34:55 25 4
gpt4 key购买 nike

Rails 版本 6,postgresql v 11,ruby 2.7.0

使用 ActiveRecord 根据表单中包含的搜索字段在 View 中填充 HTML 表时,我遇到了奇怪的行为。

如果我请求基本 URL

localhost:3000/instui/search

我得到的结果包含(正确地)数据库中的前 20 条记录(来自纽约证券交易所和纳斯达克的证券):

enter image description here

如果我在搜索字段中输入 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)

然而, View 继续呈现与前一个请求相同的结果 - 即所有记录,就好像 View 没有从 Controller 接收到新的实例变量一样。我看不出我哪里出错了。帮助表示赞赏。

enter image description here

View 是:
<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>

Controller 是:
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 %>

问题就消失了。

关于ruby-on-rails - ActiveRecord 在 View 中呈现时结果不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60230728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com