感谢您的任何帮助,您可以提供!我有一个 Ruby on Rails 应用程序,我试图在其中保存带有行车路线和航点的地图。数据需要直接来自输入表单,而不是 Google Maps javascript。我已经解决了起点和终点,但是航点给了我一个问题。
我的问题:
如何将每个航点保存在航点表中自己的记录中?我能够将第一个航路点放入表中,但其余的“选择多个”选项将被忽略。
如何使每个 waypoint.newsavedmap_id 字段与其对应的 newsavedmaps id 相同,以便以后可以调用它们?
HTML
<p>Enter a street address, city, and state:
<input id="startinput" type="text" name="starthere" size="56"></p>
<p>Or, select a location from the list:
<select id="startdrop" name="startthere">
<option value="">
<% for masterlocation in @masterlocation %>
<option value="<%= masterlocation.street_address %> <%= masterlocation.city %>, <%= masterlocation.state %>, <%= masterlocation.zip %>"><%= masterlocation.place_name %></option>
<% end %>
</select></p>
<div><b>Stops</b></div>
<div id="multiselectdiv1">
<select multiple id="waypoints" name="waypointsselected">
<% for masterlocation in @masterlocation %>
<option value="<%= masterlocation.street_address %> <%= masterlocation.city %>, <%= masterlocation.state %>, <%= masterlocation.zip %>"><%= masterlocation.place_name %></option>
<% end %>
</select>
</div>
<b>End</b>
<p>Enter a street address, city, and state:
<input id="endinput" type="text" name="endhere" size="56"></p>
<p>Or, select a location from the list:
<select id="enddrop" name="endthere">
<option value="">
<% for masterlocation in @masterlocation %>
<option value="<%= masterlocation.street_address %> <%= masterlocation.city %>, <%= masterlocation.state %>, <%= masterlocation.zip %>"><%= masterlocation.place_name %></option>
<% end %>
</select></p>
</div>
<div>
<input type="submit" onclick="calcRoute();" id="showmapview" value="Show Map">
</div>
我有两个 MySQL 表。第一个是newsavedmaps:
id
itinerary_id
start
start_lat
start_long
start_masterlocation_id
end
end_lat
end_long
end_masterlocation_id
name
第二个是航点:
id
newsavedmap_id
waypoint
waypoint_lat
waypoint_long
waypoint_masterlocation_id
两者旨在通过 newsavedmaps.id 和 waypoint.newsavedmap_id 连接。
我的 newsavedmap_controller.rb 包括:
def create
@newsavedmap = Newsavedmap.new(params[:newsavedmap])
@newsavedmap.name = params[:newsavedmapname]
if !params[:starthere].blank?
@newsavedmap.start = params[:starthere]
else
@newsavedmap.start = params[:startthere]
end
if !params[:endhere].blank?
@newsavedmap.end = params[:endhere]
else
@newsavedmap.end = params[:endthere]
end
if !params[:waypointsselected].blank?
@waypoint = Waypoint.new(params[:waypoint])
@waypoint.waypoint = params[:waypointsselected]
end
编辑 1
为了响应 Colinm 将控制器包装在迭代器中以获取每个地址的单独记录的建议,我尝试了这个,但我很确定我做错了:
if !params[:waypointsselected].blank?
for waypoint in @waypoint
@waypoint = Waypoint.new(params[:waypoint])
@waypoint.waypoint = params[:waypointsselected]
@waypoint.newsavedmap = @newsavedmap
end
end