如何将每个项目保存在一个";选择多个";字段在其自己的MySQL记录中(使用Rails)?
谢谢你能提供的任何帮助!我有一个RubyonRails应用程序,我试图保存带有驾驶方向和航路点的地图。数据需要直接从输入表单中获取,而不是从GoogleMapsJavaScript中获取。我已经解决了起点和终点,但是航路点给了我一个问题 我的问题:如何将每个项目保存在一个";选择多个";字段在其自己的MySQL记录中(使用Rails)?,mysql,ruby-on-rails,ruby,Mysql,Ruby On Rails,Ruby,谢谢你能提供的任何帮助!我有一个RubyonRails应用程序,我试图保存带有驾驶方向和航路点的地图。数据需要直接从输入表单中获取,而不是从GoogleMapsJavaScript中获取。我已经解决了起点和终点,但是航路点给了我一个问题 我的问题: 如何将每个航路点保存在航路点表中各自的记录中?我可以将第一个航路点放入表中,但其余的“选择多个”选项被忽略 如何使每个waypoint.newsavedmap\u 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>
第二个是航路点:
id
newsavedmap_id
waypoint
waypoint_lat
waypoint_long
waypoint_masterlocation_id
这两个节点通过newsavedmaps.id和waypoint.newsavedmap\u id连接
My 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
如何将每个航路点保存在航路点表中各自的记录中?我可以将第一个航路点放入表中,但其余的“选择多个”选项被忽略
除非您有很好的理由不这样做,否则请使用Rails的表单帮助程序,而不是滚动您自己的表单字段。他们将自动处理这些问题,以及处理您可能甚至不知道的边缘情况。(就像gotcha所说的那样,在没有选择的情况下发送一个multiple select back将使记录保持不变。这听起来在您的用例中不是一个问题,但是如果您使用表单帮助程序,Rails仍然支持您。)
在您的情况下,只需在html_选项哈希中传递multiple:true
,如下所示:
<%= f.select :waypointsselected,
MasterLocation.waypoints_for_select,
{},
{ multiple: true }
%>
…而params散列中该字段的键将包含一个表示所选项的值数组:
{ waypointsselected: ["3", "6", "9"] }
至于将
航路点
s与newsavedmap
s关联,只需在创建过程中明确设置即可。你就快到了:
if !params[:waypointsselected].blank?
@waypoint = Waypoint.new(params[:waypoint])
@waypoint.waypoint = params[:waypointsselected]
# You can do it like this...
@waypoint.newsavedmap = @newsavedmap
# Or using the ID...
@waypoint.newsavedmap_id = @newsavedmap.id
end
(我没有调整此代码以处理您现在在所选的
航路点中有一个值数组的事实;我只是将关系定义插入到您现有的代码中。请记住调整您的代码以期望有一个数组并对其进行迭代。)Colinm,非常感谢您的帮助!我马上就要检查你答案的第二部分,但是我在使用[]时遇到了一个问题。这将创建包含所有地址的单个记录,而不是包含单个地址的多个记录。有办法得到多张唱片吗?德拉特,你打败了我的编辑!:)我没有修改控制器代码来处理数组;Colinm,我尝试过用迭代器包装控制器,但我确信我做得不对(我对Rails非常陌生,正在尝试重新连接其他人的项目)。你能检查我的编辑吗?另外,不确定是否相关,但只有waypoint.newsavedmap=newsavedmap对我有效。由于某些原因,另一个选项没有(不过,谢谢!)
{ waypointsselected: ["3", "6", "9"] }
if !params[:waypointsselected].blank?
@waypoint = Waypoint.new(params[:waypoint])
@waypoint.waypoint = params[:waypointsselected]
# You can do it like this...
@waypoint.newsavedmap = @newsavedmap
# Or using the ID...
@waypoint.newsavedmap_id = @newsavedmap.id
end