我正在创建停车监控应用程序,尤其是对编程和 ruby on rails 完全陌生。我为 Gmaps 找到了一个很棒的扩展 - Gmaps4rails。
我有几个标记在地图上显示停车场(班级地点)的位置和拥堵情况。用户(类用户)应该能够点击标记,并且在这个弹出的小信息窗口中应该有“关注”按钮,因此用户可以随时了解停车拥堵情况。
这是我的代码
模型关系
用户
has_many :parking_relationships, :foreign_key => "watcher_id",
:dependent => :destroy
has_many :watching, :through => :parking_relationships,
:source => :watched
地点(停车场)
has_many :reverse_parking_relationships, :foreign_key => "watched_id",
:dependent => :destroy,
:class_name => "ParkingRelationship"
has_many :watchers, :through => :reverse_parking_relationships,
:source => :watcher
停车关系
belongs_to :watcher, :class_name => "User"
belongs_to :watched, :class_name => "Place"
地方控制器
def index
@user = current_user
@place = Place.find(params[:id])
@places = Place.all
@json = Place.all.to_gmaps4rails do |place, marker|
marker.infowindow render_to_string(:partial => "/places/watch_form", :locals => { :object => place})
end
respond_with @json
结尾
部分_watch_form.html.erb
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.watching?(@place) %>
<%= render 'watch' %>
<% else %>
<%= render 'unwatch' %>
<% end %>
</div>
<% end %>
部分_watch.html.erb
<%= form_for current_user.parking_relationships.
build(:watched_id => @place.id) do |f| %>
<div><%= f.hidden_field :watched_id %></div>
<div class="actions"><%= f.submit "Watch" %></div>
<% end %>
部分 _unwatch.html.erb
<%= form_for current_user.parking_relationships.find_by_watched_id(@place),
:html => { :method => :delete } do |f| %>
<div class="actions"><%= f.submit "Unwatch" %></div>
<% end %>
数据库结构
Users name, email, etc
Places Name, address, latitude, longitude, gmaps, congestion
Parking_relationships watched_id, watcher_id
这行不通。当我单击标记时,什么也没有发生,并且“观看”或“取消观看”按钮不会出现。我建议这是因为 @places 在 Places 控制器的索引操作中。
UPDATE:
Here's the app on the web
GitRepositary https://github.com/ilyacherevkov/parking_bob
Map http://parking-bob.heroku.com/places
Watchers of the parking http://parking-bob.heroku.com/places/1,2,3 etc/watchers
Users watching parkings http://parking-bob.heroku.com/users/1,2,3 etc/watching
谢谢各位