ruby-on-rails - 我该如何解决这一数据拉动问题?
问题描述
请帮忙,我有一个数据提取不适用于 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 语句可以工作并且确实记住最后一个值。请帮助我理解我编码错误的内容?
解决方案
经过进一步的实验(像个白痴一样胡思乱想),我想,如果下拉视图中的值必须与保存到数据库的数据相同怎么办?我使用的数据包含在 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 现在会记住从状态变化到状态变化!谁会呢???
推荐阅读
- android - 如何处理返回多个模型的改造调用
- node.js - Firebase推送通知Node.js 10中的可变值正确语法?
- maven - 从 Jenkins 修改 POM.xml 文件中的属性标记
- javascript - 是什么导致“无法在 'CanvasRenderingContext2D' 上执行 'drawImage' - 提供的 HTMLImageElement 处于'损坏'状态”错误?
- python-3.x - 更改 Mac 上的默认 python 版本 - 安装 Biopython
- javascript - 无法提供 defaultValue 作为表达式
- embedded-linux - 如何在 Yocto 中排队补丁
- python - 在python中调用n级嵌套列表中的值
- python - 我从给定的字符串中找到一封电子邮件并返回字符串中存在的电子邮件
- ios - 如何根据不同的布尔值更改变量?