【发布时间】:2018-03-04 23:31:56
【问题描述】:
有没有办法使用 gmaps4rails gem 查看标记,具体取决于当前用户登录?我不确定它是否有可能成功,但我尝试结合使用康康宝石,但它只会导致所有标记消失。不幸的是,我不知道如何继续。所以我想要实现的是根据当前登录的用户使标记可见。我很感激任何帮助!
relationship_controller 的当前代码:
def index
@relations = Relation.all
@hash1 = Gmaps4rails.build_markers(@relations) do |relation, marker|
# something like "if current_user == relation.user_id" ?
marker.lat relation.childlat
marker.lng relation.childlng
marker.picture({
:url => ActionController::Base.helpers.image_path("childgross.png"),
:width => "45",
:height => "45"
})
end
@hash2 = Gmaps4rails.build_markers(@relations) do |relation, marker|
marker.lat relation.kigalat
marker.lng relation.kigalng
marker.picture({
:url => ActionController::Base.helpers.image_path("persongross.png"),
:width => "45",
:height => "45"
})
end
end
关系索引视图中的代码:
<%=raw @hash1.to_json %>
<%=raw @hash2.to_json %>
<script>
handler = Gmaps.build('Google');
handler.buildMap({provider: {},internal: {id: 'map'}},function(){
markers = handler.addMarkers(<%=raw @hash1.to_json %>);
markers = handler.addMarkers(<%=raw @hash2.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
【问题讨论】:
-
您想根据用户更改标记?或者显示或不显示标记?这两种选择都是可能的。为什么不显示创建标记的代码?
-
@Pablo 这是个好问题。我编辑了我的问题并添加了代码。不要混淆我有两个哈希值,一个用于为儿童创建标记,一个用于为幼儿园的标记创建(我不确定是否这样做)。我希望根据用户显示孩子。因此,如果孩子不属于当前用户,则标记不应显示在地图中。我删除了我的 cancan 方法,因为它不起作用..
-
在我看来,您只能使用属于 current_user 的关系创建第一个哈希。和所有关系的第二个哈希。根据您的模型使用“@user_rel = current_user.relations”或“@user_rel = Relation.where(user_id: current_user.id)”。
-
非常感谢!它有效:)
标签: ruby-on-rails google-maps google-maps-markers cancan gmaps4rails