remote:true是否影响参数如何发送到控制器?

时间:2018-08-29 01:49:44

标签: ruby-on-rails ruby parameters ruby-on-rails-5

过去两天来,我一直在为此苦苦挣扎,但我很沮丧。我有一个简单的form_tag,它使用remote: true助手与ajax一起提交。我一直在尝试使用params[:query]从控制器中的表单访问值,但是我一直在获取nil

_form.html.erb

<%= form_tag queries_path, method: :get, remote: true do %>
    <%= text_field_tag :query %><br>
    <%= submit_tag 'Search', name: nil, class: 'submit' %>
<% end %>

以上:我认为似乎很简单。

queries_controller.rb

def index
    @search_term = params[:query] #value is nil

    gon.queries = Query.api_response(@search_term)
    respond_to do |format|
      format.js
      format.html
    end
  end

application.html.erb

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
    <%= stylesheet_link_tag    "application" %>
    <%= Gon::Base.render_data %>
    <%= include_gon(:init => true) %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/plug-ins/1.10.19/api/sum().js"></script>
        <link href="https://fonts.googleapis.com/css?family=Righteous" rel="stylesheet">
  </head>

  <%= render 'shared/nav' %>

  <body>
    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>

    <%= yield %>

  </body>

</html>

控制台日志

Started GET "/queries" for 148.75.220.61 at 2018-08-29 01:26:01 +0000
Cannot render console from 148.75.220.61! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by QueriesController#index as HTML
  Rendering queries/index.html.erb within layouts/application
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendered shared/_form.html.erb (1.3ms)
  Rendered queries/index.html.erb within layouts/application (6.9ms)
  Rendered shared/_nav.html.erb (1.5ms)
Completed 200 OK in 1180ms (Views: 113.7ms | ActiveRecord: 0.8ms)


Started GET "/queries?utf8=%E2%9C%93&query=egg&search=" for 148.75.220.61 at 2018-08-29 01:26:07 +0000
Cannot render console from 148.75.220.61! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by QueriesController#index as JS
  Parameters: {"utf8"=>"✓", "query"=>"egg", "search"=>""}
  Rendering queries/index.js.erb
  Rendered queries/index.js.erb (0.8ms)
Completed 200 OK in 1132ms (Views: 6.6ms | ActiveRecord: 0.0ms)

我注意到当remote: true方法帮助程序不处于表单中时,我可以在控制器中访问表单的参数,但这需要页面加载并丢失参数。我觉得也许我想念一些东西。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

从日志中,我看到index动作有2个请求。

一个没有查询参数的参数,我确信您正在访问index页面而没有查询。另一个是查询参数。您总是会得到前一个nil,因为您没有在请求中传递任何内容。

因此,理想情况下,当找不到搜索字词时,您应该列出所有资源。所以我建议类似的解决方案

def index
    @search_term = params[:query] #value is nil
    if @search_term.present?
      gon.queries = Query.api_response(@search_term)
    else
      gon.queries = {} # Or anything which makes sense when search term is not found
    end
    respond_to do |format|
      format.js
      format.html
    end
end