Rails get parameter is nil in production

Viewed 410

I have a simple search form:

<%= 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 %>

It redirects to this controller method:

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

Which uses this model scope:

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

Everything works fine in development but in production I get the following error:

no implicit conversion of nil into String

It seems the params[:q] returns nil, but I can't find why?

EDIT

Here is the HTML form rendered (see it in action 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>
1 Answers

First lookin your server log to make sure your params are actually getting passed from the web page. You should see something like the following (only specific to YOUR params, this is just from an app I had running:

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

Then make sure the params are actually available in the controller. I would remove all unnecessary code to make sure that your assumptions are right about params['q'] being nil. You don't need to be doing any Word.search(params['q']).first(20). That just increases the surface area to debug. I'd personally just log params['q'] to my server log to make sure it's there.

If it is in fact sent to the server, and nil in the controller, it's probably a strong parameters issue and needs to be be permitted.

If you find that params['q'] isn't in fact nil in the controller, the error may be coming from your search code.

Another thing that comes to mind is that "strong params" is somewhat configurable in different versions of Rails. I'm not sure what version you're on, but you might have a config setting like one of the following, that is different in production and development:

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