You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails中simple_form下拉框显示对象十六进制地址问题求助

问题解决方法

你遇到的下拉显示对象十六进制地址的问题,是因为simple_form渲染关联下拉时,默认会调用模型对象的to_s方法,而Rails模型默认的to_s输出就是对象的内存地址标识。解决方法有两种:

方法一:全局设置Systemname对象的显示文本

在Systemname模型中添加to_s方法,指定用sname字段作为对象的显示文本,这样所有用到Systemname对象显示的地方都会生效:

class Systemname < ApplicationRecord
  has_many :raports

  def to_s
    sname
  end
end

方法二:仅在当前表单中指定显示字段

如果不想全局修改,也可以在f.association调用里显式指定label_method参数,直接指定用sname字段作为下拉选项的显示文本:

<%= simple_form_for @raport do |f| %>
  <%= f.association :systemname, prompt: "---choose one---", label_method: :sname %>
  <%= f.input :rsi %>
  <%= f.input :crit %>

  <%= f.submit class: "btn btn-primary btn-sm" %>
<% end %>

内容的提问来源于stack exchange,提问作者mila002

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 18:12:02