rails实现了事务搜索路由配置



相当新的rails开发人员,仍在努力了解事情的发展方向以及如何连接它们。

我有一个"记录"数据库,正在搜索它们。我找到了transak 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)

-更新--将搜索更改为"get"解决了这个问题,并确保使用@q而不是自定义名称,而不更改transak配置以匹配。