2016-12-05 4 views
0

위치에 따라 @school 오브젝트 근처에 오브젝트 @center를 찾아 맵 박스에 센터를 표시하려고합니다. 나는 여기에 내 코드를지나 내가 무슨 짓을했는지 모르겠어요 때문에 여기 레일, 맵 박스 및 지오 코더 : 다른 오브젝트 근처에있는 오브젝트 찾기

<div id="school-map" data-coordinates="<%= @school.coordinates.reverse %>" data-centers="<%= @center.coordinates.reverse %>"> </div> 

가 center.rb 모델입니다 : 여기

가 (: 학교 및 센터 2 데이터) 코드를 표시하는지도입니다 :

def center_near_school(school_adress_coordinates) 
school_adress_coordinates = @school.coordinates.reverse 
center = Center.near(school_adress_coordinates, 1, units: :km) 

:

school.rb 모델에서
class Center 
    include Mongoid::Document 
    include Geocoder::Model::Mongoid 

    has_many :schools 
    accepts_nested_attributes_for :schools 

    field :title,    type: String 

    field :image_path,  type: String 
    field :Attachment_path, type: String 
    field :website,   type: String 

    field :phone,    type: String 

    field :street,   type: String 
    field :zipcode,   type: String 
    field :city,    type: String 
    field :coordinates,  type: Array 

    geocoded_by :adress 
    #after_validation :geocode 

    def adress 
    [street, zipcode, city].compact.joint(', ') 
    end 

end 

,이 방법을 시도 (: 학교, has_many : 센터 has_many) 두 모델 모두, ​​마커에서

<% if @school.center_near_school %> 
      <p> OUI </p> 
      <% else %> 
      <p> NON </p> 
      <% end %> 

내가 관계를 만들 : 16,

와 나는 내 방식 일이 있는지 알고 "예"또는 "아니오"와 같은 문자열을 게재하도록 노력 map을 표시하는 show.js에서 생성됩니다.

누군가이 기능을 만드는 몇 가지 해결책을 찾도록 도와 줄 수 있습니까?

는 (... 코드, 정밀도) 더 많은 정보와이 게시물을 편집달라고

감사를 주저하지 마십시오!

+0

'join'이 아니라'join'입니다. 이미 효과가있는 것은 무엇입니까? '가까운 '방식으로 쓰고 싶습니까? –

+0

@EricDuminil 메소드가 이미 존재하는 지오 코더 메소드입니다. –

+0

So. 무슨 일 이죠, 안그래? 어떤 오류가 있습니까? –

답변

1

Center.near(school_adress_coordinates, 1, units: :km)

에는 센터가 발견되지 않으면 비어있을 수 센터의 배열을 반환합니다.

귀하의 방법은 centers_near_school를 호출해야합니다, 당신은 사용할 수 있습니다 루비에서

<% if @school.centers_near_school.empty? %> 
    <p> NON </p> 
<% else %> 
    <p> OUI </p> 
<% end %> 

은, 모든 객체는 "truthy"입니다, falsenil 제외. 빈 배열이라도 if 문에서 true로 간주됩니다.

+0

감사합니다. 좋은 결과를 얻었습니다. –