Rails get参数在生产中为空

问题描述

我有一个简单的搜索表:

<%= form_with(url: recherche_path,method: "get",class: "side-box",local: true) do %>
    <div id="search-container">
        <%= text_field_tag(:q,nil,placeholder: "Rechercher un mot dans le dictionnaire...",id: "search") %>
        <ul id="search-results"></ul>
    </div>
<% end %>

它重定向到此控制器方法:

def recherche
    @results = Word.search(params['q']).first(20)
end 

使用此模型范围:

scope :search,->(content) {
quoted_content = ActiveRecord::Base.connection.quote_string(content)
where("content % :content",content: content).
  order(Arel.sql("similarity(content,'#{quoted_content}') DESC"))
}

在开发中一切正常,但在生产中出现以下错误:

没有将nil隐式转换为String

似乎params [:q]返回nil,但是我找不到原因?

编辑

这是呈现的HTML表单(在操作here中查看):

<form class="side-box" action="/dictionnaire/recherche" accept-charset="UTF-8" data-remote="true" method="get">
        <div id="search-container" data-children-count="1">
            <input type="text" name="q" id="search" placeholder="Rechercher un mot dans le dictionnaire..." class="">
            <ul id="search-results"></ul>
        </div>
</form>

解决方法

首先查看服务器日志,以确保您的参数实际上已从网页传递。您应该看到类似以下的内容(仅针对您的参数,这仅来自于我运行的应用程序:

Started GET "/trips/algeria" for ::1 at 2020-08-31 15:42:35 -0700
Processing by TripsController#search as HTML
  Parameters: {"facets"=>"algeria"}

然后确保参数在控制器中实际可用。我将删除所有不必要的代码,以确保您对params['q']为零的假设是正确的。您无需做任何Word.search(params['q']).first(20)。这只会增加调试的表面积。我个人只是将params['q']登录到服务器日志中以确保它在那里。

如果实际上已将其发送到服务器,并且在控制器中为nil,则可能是一个很重要的参数问题,需要允许。

如果您发现控制器中的params['q']实际上不是nil,则错误可能来自您的搜索代码。

我想到的另一件事是,“强参数”在不同版本的Rails中是可配置的。我不确定您使用的是哪个版本,但是您可能具有类似以下配置之一的配置设置,在生产和开发中会有所不同:

config.active_record.whitelist_attributes =
config.action_controller.permit_all_parameters =

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...