首页 > 解决方案 > 我该如何解决这一数据拉动问题?

问题描述

请帮忙,我有一个数据提取不适用于 rails 5.2 中的 collection_select,我找不到我做错了什么。

使用 rails 5.2 和 ruby​​ 2.6.1 的新应用程序,只有这个代码片段无法执行。

        #---- called by views/myoffer/_form.html.erb
        #---code shown below
module MyoffersHelper

    @my_scosts =    Setmeup.find_by_sql("select (t_is || '--' || description) as shipr, description as cost  from setmeups where active is true and g_is = 'Ship' order by t_is, srby;")

    end
end
      #----- extracted from _form.html.erb
<%= form_with(model: myoffer, local: true) do |form| %>
        # lots of stuff, including 
        # working collection_select 

<div class="field">
<% @my_scosts = "" %>
<% get_scost %>
<%= @myoffer.shipping_quoted %>  # proves shipping_quoted has value
  <%= form.label :shipping_quoted %>
  <%= form.collection_select :shipping_quoted, @my_scosts, :cost, :shipr%>
</div>

        # data as pulled by sql
        FedEx_Large--17.00   | 17.00
         FedEx_Medium--7.00   | 7.00
         FedEx_Small--5.00    | 5.00
         Self_Delivery--1.00  | 1.00
         USPS_2d_Large--9.00  | 9.00
         USPS_2d_Medium--5.00 | 5.00
         USPS_2d_Small--3.00  | 3.00

此 collection_select 显示并分配正确的值,但不记住最后选择的值。(值确实存在,如 form.label 上方的调试语句所示。)我还有其他几个 find_by_sql collection_select 语句可以工作并且确实记住最后一个值。请帮助我理解我编码错误的内容?

标签: ruby-on-railsruby

解决方案


经过进一步的实验(像个白痴一样胡思乱想),我想,如果下拉视图中的值必须与保存到数据库的数据相同怎么办?我使用的数据包含在 Setmeup 表的文本框中,myoffer 表中的 shipping_quoted 字段是十进制的。我将sql查询更改如下:

@myscosts = Setmeup.find_by_sql("select (t_is || '--' || description) as shipr, description::float as cost  from setmeups where active is true and g_is = 'Ship' order by t_is, srby;")

并且 collection_select 现在会记住从状态变化到状态变化!谁会呢???


推荐阅读