0

Rails は初めてで、Rails 2.3.14 サイトの問題を解決しようとしています。問題は、私が修正しようとしているこの店舗ロケーターが戻ってくることです

nil:NilClass の未定義のメソッド「coordinates」

距離パラメータ内に店舗が見つからない場合。見つかった場合は、正常に動作します。

コントローラーで作業しようとしている現在のコードは次のとおりです

  @map = GMap.new("locations-gmap-div", "locationsGMap")
  @map.control_init(:large_map => true, :map_type => true)
  @mapp.center_zoom_init(@locations.first.coordinates, 8)

これが私のコードでやろうとしたことです。私はまだ Rails に非常に慣れていないので、ここでフィールドから外れていたら申し訳ありません。

 @map = GMap.new("locations-gmap-div", "locationsGMap")
 @map.control_init(:large_map => true, :map_type => true)

if @map.center_zoom_init(@locations.first.coordinates,8).nil?
    flash[:error] = 'Sorry, we could not find any stores matching that criteria.'  
    redirect_to store_locator_path          
else
    @map.center_zoom_init(@locations.first.coordinates,8)

end

どんな助けでも大いに感謝します。

4

2 に答える 2

1

@locations が空であるため失敗します。

@locations は [] を返します

@locations.first は nil を返します

nil.coordinates は、nil:NilClass に対して未定義のメソッド「coordinates」を発生させます

if @locations.empty?
    flash[:error] = 'Sorry, we could not find any stores matching that criteria.'  
    redirect_to store_locator_path          
else
    @map = GMap.new("locations-gmap-div", "locationsGMap")
    @map.control_init(:large_map => true, :map_type => true)
    @map.center_zoom_init(@locations.first.coordinates,8)
end
于 2013-06-05T15:48:32.793 に答える
1

を初期化していません@locations.first。プログラムは、存在しないものの座標にアクセスしようとしているようです (したがって、例外が on である理由nil:NilClass)。

于 2013-06-05T15:49:45.520 に答える