Я пытаюсь получить данные для модели rider
из модели location
и передать ее в часть javascript, чтобы я мог отметить каждое из мест нахождения гонщиков.
Я попытался задать правильный вопрос относительно того, что я намеревался сделать, но не повезло.
Вот моя модель rider.rb
.
class Rider < ActiveRecord::Base
has_one :location, as: :profile
end
И вот моя модель location.rb
.
class Location < ActiveRecord::Base
geocoded_by :address
belongs_to :profile, polymorphic: true
validates :profile, :latitude, :longitude, presence: true
end
Вот мои controllers.rb
.
def show
@riders = Rider.all
end
И вот мой show.html.haml
:javascript
var map;
var riders = #{raw @riders.to_json}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 3, lng: 102 },
zoom: 9
});
riders.forEach(function(rider) {
var marker = new google.maps.Marker({
position: { lat: rider.latitude, lng: rider.longitude }, //this line does not work
map: map,
animation: google.maps.Animation.DROP,
title: rider.name.split(' ').slice(0,2).join(' '),
label: rider.name.split(' ').slice(0,2).join(' '),
});
}
Когда я console.log(riders)
, это результат, не хватает необходимых атрибутов location
.
authentication_token: "admin_token_16"
branch_id: 55
deposit_cents: 1000000
email: "[email protected]"
id: 19
name: "Leilani Mosciski Corkery"
online: false
order_need_deposit: true
phone:"+9860128134706"
status: "active"
working: false
Любая помощь или понимание глубоко оценены!
Вы не получаете данные о местоположении гонщиков в функции show. Таким образом, замените вашу функцию show на один ниже
def show
@riders = Rider.joins(:location).select('riders.*','locations.latitude','locations.longitude')
end
location
должно быть единственного числа. Upvoted. Если вам случится поехать в Малайзию, дайте мне сообщение! Lol Вы редактировали это до меня!