mysql - 如何将 "select multiple"字段中的每个项目保存在其自己的 MySQL 记录中(使用 Rails)?

标签 mysql ruby-on-rails ruby

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

我的问题:

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

  2. 如何使每个 waypoint.newsavedmap_id 字段与其对应的 newssavedmaps 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

两者旨在通过 newssavedmaps.id 和 waypoint.newsavedmap_id 连接。

我的 newssavedmap_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 将 Controller 包装在迭代器中以获取每个地址的单独记录的建议,我尝试了此操作,但我很确定我做错了:

    if !params[:waypointsselected].blank?
  for waypoint in @waypoint
  @waypoint = Waypoint.new(params[:waypoint])
  @waypoint.waypoint = params[:waypointsselected]
  @waypoint.newsavedmap = @newsavedmap
  end
end

最佳答案

How can I save each waypoint in its own record in the Waypoint table? I'm able to get the first waypoint into the table, but the rest of the "select multiple" options are ignored.

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

就您而言,您只需在 html_options 哈希中传递 multiple: true 即可,如下所示:

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

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


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

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

...参数哈希中该字段的键将包含代表所选项目的值数组:

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


至于将waypointnewsavedmap关联起来,只需在创建时明确设置即可。你就快到了:

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 中有一个值数组的事实;我只是将关系定义插入到您现有的代码中。请记住将您的代码调整为期望一个数组并迭代它。)

关于mysql - 如何将 "select multiple"字段中的每个项目保存在其自己的 MySQL 记录中(使用 Rails)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912310/

相关文章:

返回 null 的 MySQL 计数(列)

PHP - 在 MySQL 查询中跳过显示重复字段(不是记录)

php - 当 mysql_query 返回 false 时

ios - 设计:iOS “Add to Home Screen”

mysql - 在 Rails 中,数据库调用在开发中有效,但在测试中停滞

ruby - 方法调用中括号的使用规则是什么?

ruby - 如何在 ruby​​ 中实现 curry(部分函数)

ruby-on-rails - Rails - 唯一性导致 "Stack Too Deep"错误

php - 使用 php mysql 和 inner join 重复结果

ruby-on-rails - 如何在 Rails 应用程序中扩展 Time 类?