nil:NilClass 的未定义方法 `gsub' -> 请求参数:None

undefined method `gsub' for nil:NilClass -> Request Parameters: None

我尝试创建搜索和过滤表单。一切正常,除了当我刷新页面时,nil:NilClass'

出现错误 'undefined method `gsub'

当我有 url 这样的时候 :

-> http://localhost:3000/places(错误)

-> http://localhost:3000/places?utf8=%E2%9C%93&query=&filter=Select+Filter&commit=%F0%9F%94%8E(有效)

-- VIEW --
<%= form_for :search, url: places_path, class: "search-form", method: :get do %>
    <%= text_field_tag :query, params[:query],
          class: "search-input form-control placeholder-search",
          placeholder: "Location.."%>
    <%= select_tag "filter", options_for_select(['Select Filter',
                  'Cafe',
                  'Coffee Shop',
                  'Art Gallery']), class:'custom-select' %>
    <%= submit_tag ""  %>
  <% end %>

-- CONTROLLER -- 
@places = policy_scope(Place)
    @places = Place.all
    query = params[:query]
    @places = query.present? ? Place.global_search(query) : Place.all

    if params[:filter] == 'Select Filter'
      @places = results
    else
      # 'Art Gallery' -> 'Art_Gallery' -> 'art_gallery' -> :art_gallery
      symbol = params[:filter].gsub(/ /, '_').downcase!.to_sym
      # @places = results.where(:art_gallery => true)
      @places = @places.where(symbol => true)
    end

params[:filter]nil,因此您不能对其调用 gsub。空白过滤器似乎与过滤器 Select Filter 属于同一类别,因此我会将代码更改为:

if params[:filter].blank? || params[:filter] == 'Select Filter'
  @places = results
else
  # 'Art Gallery' -> 'Art_Gallery' -> 'art_gallery' -> :art_gallery
  symbol = params[:filter].gsub(/ /, '_').downcase.to_sym
  # @places = results.where(:art_gallery => true)
  @places = @places.where(symbol => true)
end