希望这会很简单。但我现在已经狩猎了几个小时,似乎无法让它发挥作用。我有多个地址的用户,我正在尝试使用 Geocoder gem 通过邮政编码搜索显示这些用户,我的代码与 Geocoder Railscast 中的代码非常相似。
这是我的控制器尝试 1 并返回“未定义的方法‘地址’”
def index
if params[:search].present?
@profiles = Profile.Addresses.near(params[:search], 25, :order => :distance)
@title = "Therapists Near " + :search
else
@profiles = Profile.all
@title = "Everyone"
end
end
这是第 2 次尝试,这将返回“未初始化的常量 ProfilesController::Addresses”(我不知道 Profile.where 位是否有效,但它甚至没有到达那部分......)
class ProfilesController < ApplicationController
def index
if params[:search].present?
addresses = Addresses.near(params[:search], 25, :order => :distance)
@profiles = Profile.where(:id => addresses.id)
@title = "Therapists Near " + :search
else
@profiles = Profile.all
@title = "Everyone"
end
end
这是我的模型:
class Profile < ActiveRecord::Base
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses, :reject_if => lambda { |a| a[:street].blank? }, :allow_destroy => true
class Address < ActiveRecord::Base
belongs_to :profile
geocoded_by :street
after_validation :geocode, :if => :street_changed?
非常感谢您的观看!