为什么此形式的GET请求不会将浏览器发送到呈现的视图?

时间:2018-06-21 22:47:02

标签: ruby-on-rails

我为以下控制器定义了以下路由get 'search', to: 'search#show'

class SearchController < Application Controller
  def show
    @users = User.where( { username: params[:query] } )
    respond_to do |format|
      format.html
    end
  end
end

我有一个views/application/_search.html.erb上的搜索表

<%= form_with method: :get, url: search_path do |form| %>
  <div class="field">
    <%= form.text_field :query, placeholder: "Search users" %>
  </div>
<% end %>

它在layouts/_header.html.erb内以<%= render "search" %>行呈现。

每执行一次搜索,都会得到以下控制台输出:

Started GET "/search?utf8=%E2%9C%93&query=hello" for 127.0.0.1 at 2018-06-21 18:37:20 -0400
Processing by SearchController#show as JS
  Parameters: {"utf8"=>"✓", "query"=>"hello"}
  Rendering search/show.html.erb within layouts/application
  Rendered search/show.html.erb within layouts/application (0.3ms)
  Rendered application/_logo.html.erb (1.2ms)
  Rendered application/_search.html.erb (0.8ms)
  Rendered application/_flashes.html.erb (0.5ms)
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/helpers/sessions_helper.rb:7
  Rendered layouts/_header.html.erb (242.0ms)
  Rendered layouts/_footer.html.erb (5.1ms)
Completed 200 OK in 407ms (Views: 366.0ms | ActiveRecord: 1.8ms)

页面似乎正在渲染,但是我的浏览器没有任何显示。为什么是这样?当前view/search/show.html.erb仅包含Rails生成的默认文本。

1 个答案:

答案 0 :(得分:1)

因为它是作为JS呈现/响应的。

  

通过SearchController#show以JS处理

您需要使用HTML进行响应,以便浏览器可以直接呈现,也可以使用一些前端JavaScript来处理响应。