【发布时间】:2022-08-17 08:55:45
【问题描述】:
相当新的 Rails 开发人员,并且仍在试图弄清楚事情的去向以及如何连接它们。
我有一个 \'records\' 的数据库,并且正在寻找它们。我找到了 ransack gem,它可以做到这一点,但是我不想将搜索放在索引页面上,我想要一个单独的页面用于搜索和它的结果。
我在记录控制器中创建了一个新操作:
def search
@q = Record.ransack(params[:q])
@found_records = @q.result(distinct: true)
end
然后是 search.html.erb 视图,然后是路线:
resources :records do
match :search, to: \'records#search\', on: :collection, via: [:get, :post]
end
然后是视图本身
<%= search_form_for(
@q,
url: search_records_path,
html: { method: :post }
) do |f| %>
<%= f.label :brief %>
<%= f.search_field :brief %>
<%= f.submit %>
<% end %>
<div id=\"records\">
<% @found_records.each do |record| %>
<%= render record %>
<% end %>
</div>
这运行没有错误,但是当我按下搜索框时,页面只是刷新,没有执行搜索。
我想这是一个路由问题,但现在确定如何设置搜索按钮使用的路由?非常感谢这里的任何建议!
- 编辑 日志对我来说看起来不错,这是控制台上记录的内容。
Started POST \"/records/search\" for 127.0.0.1 at 2022-08-09 05:35:52 +0800
Processing by RecordsController#search as HTML
Parameters: {\"authenticity_token\"=>\"[FILTERED]\", \"q\"=>{\"brief\"=>\"rain\"}, \"commit\"=>\"Search\"}
Rendering layout layouts/application.html.erb
Rendering records/search.html.erb within layouts/application
Record Load (0.1ms) SELECT DISTINCT \"records\".* FROM \"records\"
↳ app/views/records/search.html.erb:20
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 49)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 47)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 48)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 47)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 49)
Rendered records/search.html.erb within layouts/application (Duration: 4.5ms | Allocations: 1984)
Rendered layouts/_shim.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layouts/_header.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layout layouts/application.html.erb (Duration: 24.0ms | Allocations: 7469)
Completed 200 OK in 26ms (Views: 24.7ms | ActiveRecord: 0.1ms | Allocations: 8216)
标签: ruby-on-rails ransack