0

感谢您的任何帮助,您可以提供!我有一个 Ruby on Rails 应用程序,我试图在其中保存带有行车路线和航点的地图。数据需要直接来自输入表单,而不是 Google Maps javascript。我已经解决了起点和终点,但是航点给了我一个问题。

我的问题:

  1. 如何将每个航点保存在航点表中自己的记录中?我能够将第一个航路点放入表中,但其余的“选择多个”选项将被忽略。

  2. 如何使每个 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
4

1 回答 1

0

如何将每个航点保存在航点表中自己的记录中?我能够将第一个航路点放入表中,但其余的“选择多个”选项将被忽略。

除非您有充分的理由不这样做,否则请使用 Rails 的表单助手而不是滚动您自己的表单字段。他们会自动处理这个问题,并处理你甚至可能不知道的边缘情况。(就像在没有选择的情况下发送多重选择会使记录保持不变的问题一样。这听起来不像您的用例中的问题,但如果您使用表单助手,Rails 仍然支持您。)

在您的情况下,您只需传入multiple: truehtml_options 哈希,如下所示:

<%= f.select :waypointsselected,
    MasterLocation.waypoints_for_select,
    {},
    { multiple: true } 
%>

请注意,这确实假设您实现了一个waypoints_for_select方法。在没有看到所有代码的情况下,您现在的视图中似乎有太多的逻辑。在视图中构造选项数组既脆弱又冗长;将其卸载到模型或助手可以使您的视图代码更清晰,并有助于消除潜在的未来错误。


如果您绝对不能/绝对不想在此应用程序中使用表单助手,那么您正在寻找的关键是[]. Rails 明智地假定表单字段表示单个值,除非您明确将其表示为数组。只需在字段名称的末尾添加一个空数组:

<select multiple id="waypoints" name="waypointsselected[]">

...并且该字段在 params 哈希中的键将包含一个表示所选项目的值数组:

{ waypointsselected: ["3", "6", "9"] }


至于将waypoints 与newsavedmaps 关联起来,只需在创建时显式设置即可。您快到了:

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

(我没有调整这段代码来处理你现在有一个值数组的事实waypointsselected;我只是将关系定义插入到你现有的代码中。请记住调整你的代码以期望一个数组并对其进行迭代。)

于 2013-07-28T21:41:34.573 回答