我正在创建停车监控应用程序,特别是我对编程和Ruby上的全新操作。我找到了Gmaps的一个很好的扩展 - Gmaps4rails。
我有几个标记显示地图上的停车位置和拥挤情况(Class Place)。用户(类用户)应该能够点击标记,在这个小弹出窗口中应该有“跟随”按钮,这样用户就可以了解停车拥堵情况。
这是我的代码
模型关系
用户
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
ParkingRelationship
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
这不起作用。当我点击标记时,没有任何反应,并且“Watch”或“Unwatch”按钮不会显示。我建议这是因为@place在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
谢谢你们,伙计们