Rails中基于params的条件判断失效问题求助
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
searchparam), it defaults to all facilities—no nil errors because we checkparams[:search].present?first.
Step 2: Fix the Global Map Link
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.
Step 3: Optional (But Recommended): Strong Parameters
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 meantparams[:search][:city_id].blank?), you skipped checking ifparams[: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
searchparam, 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

