You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails中基于params的条件判断失效问题求助

Fixing Dual Map Functionality (City vs Global Facilities) in Rails

Let's break down what's going wrong and fix this step by step:

The Core Problem

Your main issue is that when loading the global map, params[:search] doesn't exist at all—so trying to access params[:search][:city_id] throws that undefined method [] for nil:NilClass error. Plus, your global map link is generating wonky parameters (that weird format value looks like an accidental ActiveRecord relation being passed instead of a valid format).

Step 1: Fix the Controller Logic

We need to reorder our checks to first verify that the search parameter exists, then check if a valid city_id is present. Here's the corrected maps method:

def maps
  # First, check if we have a valid city search parameter
  if params[:search].present? && params[:search][:city_id].present? && params[:search][:city_id].to_i.positive?
    # Load only facilities for the specified city
    @facilities = Facility.joins(:city).where(city_id: params[:search][:city_id])
  else
    # Load ALL facilities for the global map
    @facilities = Facility.all
  end

  # Your existing Mapbox rendering code goes here...
end

This way:

  • When the city map is loaded (with search[:city_id] present), it pulls only that city's facilities
  • When the global map is loaded (no search param), it defaults to all facilities—no nil errors because we check params[:search].present? first.

That strange format parameter in your global map request tells me your link to the global map is broken. You're probably accidentally passing an ActiveRecord object (like @facilities) into the format param by mistake.

Make sure your global map link looks like this (no extra params):

<%= link_to "View Global Facilities Map", facilities_maps_path %>

Avoid any links that pass format: @facilities or other invalid parameters—those are causing the wonky format value in your request.

Your params are marked as permitted: false, which is a security red flag. Add a strong params method to clean this up:

private

def search_params
  # Only permit city_id if the search param exists
  params.require(:search).permit(:city_id) if params[:search].present?
end

Then update your maps method to use it:

def maps
  if search_params.present? && search_params[:city_id].present? && search_params[:city_id].to_i.positive?
    @facilities = Facility.joins(:city).where(city_id: search_params[:city_id])
  else
    @facilities = Facility.all
  end
end

Why Your Previous Logic Failed

  • When you tried if "city_id".blank? (I think you meant params[:search][:city_id].blank?), you skipped checking if params[:search] exists first. So when the global map loads, params[:search] is nil, and calling [:city_id] on it crashes.
  • Your earlier if-else either didn't account for the nil search param, or had the conditions reversed, leading to global data showing up in city maps.

This setup should have both maps working correctly: city maps show only local facilities, and the global map shows everything.

内容的提问来源于stack exchange,提问作者Maria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:53:15