Ruby on rails 如何在其他视图中显示Rails选择字段值而不是存储的整数

Ruby on rails 如何在其他视图中显示Rails选择字段值而不是存储的整数,ruby-on-rails,Ruby On Rails,我在Rails应用程序中使用了一个选择字段,该字段不绑定到相关模型,而是存储一系列静态选项的整数值,即 <%= select (:this_model, :this_field, [['Option1',1],['Option2',2],['Option3',3],['Option4',4]] ) %> 发票显示视图: Payment Status: <%= @invoice.text_for_payment_status %> 付款状态: 在控制台中: irb &

我在Rails应用程序中使用了一个选择字段,该字段不绑定到相关模型,而是存储一系列静态选项的整数值,即

<%= select (:this_model, :this_field, [['Option1',1],['Option2',2],['Option3',3],['Option4',4]] ) %>
发票显示视图:

Payment Status: <%= @invoice.text_for_payment_status %>
付款状态:
在控制台中:

irb > i=Invoice.find(4)
=> [#<Invoice id: 4, payment_status: 1 >]
irb > i.text_for_payment_status
=> nil
irb>i=Invoice.find(4)
=> [#]
irb>i.text\u用于支付状态
=>零

我已经尝试过在键周围定义带引号和不带引号的散列。我遗漏了什么?

类似的东西可以:

<%= form_for @my_model_object do |form| %>
  <%= form.label :column_name "Some Description" %>
  <%= form.select :field_that_stores_id, options_for_select({"text1" => "key1", "text 2" => "key2"}) %>
<% end %>
但是你最好把这个散列存储在像模型一样的中心位置

class MyModel < ActiveRecord
  @@my_select_something_data = {"key1" => "text 1", "key2" => "text2"}
  def text_for_something_selectable
    @@my_select_something_data[field_that_stores_id]
  end
end
这有许多可能的变化。但这应该是可行的,你可以把所有的信息放在一个中心位置

更新

好的,我在我们的网站上用了类似的东西。我们需要为rma存储返回_头。它们需要将返回原因存储为代码。这些代码在外部MS SQL Server数据库中定义(网站与该数据库交换大量数据,如订单、产品等)。在externaldb表中存储的返回原因比我实际需要的要多得多,所以我只取出了其中的一些。仍然必须确保代码是正确的

因此,他的模型是:

class ReturnHeader < AciveRecord::Base
  @@return_reason_keys = {"010" => "Wrong Produc",
                          "DAM" => "Damaged",
                          "AMT" => "Wrong Amount"}

  def self.return_reason_select
    @@return_reason_keys.invert
  end

  def return_reason
    @@return_reason_keys[nav_return_reason_code]
  end
end

如果运行时遇到问题,请检查键的类型和值是否正确。可能在方法中添加一些带有logger.info的调试信息,以查看实际使用的数据。

谢谢Thorsten,我需要在show或index视图中显示表单的结果。因此,这将是一个静态文本字段,而不是带有选择字段的表单。我正在寻找类似于布尔值改变方式的东西
。有什么想法吗?太好了,看起来很有希望!我遵循了您的示例,但它根本不返回任何值,只是空白。我肯定我在什么地方犯了个错误。我在哪里可以找到更多关于这个的信息,或者我应该在谷歌上搜索什么?@Andy Harvey:你可以用符号来表示键,比如:key1,而不是字符串,比如“key1”。您的数据库很可能存储字符串(也可能使用简单的int)。等一下,我在soe项目中也有类似的东西,很快就会给出一个更详细的例子。谢谢Thorsten,我觉得这真的很接近,但我的rh.return\u reason等价物仍然返回零。我不明白为什么我的代码跟你的一样,我已经尝试了我能想到的所有合理的排列。我的密钥作为整数存储在数据库中。还有其他想法吗?我在上面发布我的代码。我不知道为什么,但现在使用我/你的原始代码就可以了。可能是服务器重新启动的结果?谁知道呢,但是谢谢,谢谢,谢谢!!您可以在文本中添加
put payment\u status
“for\u payment\u status”方法,以查看获得的值。或者只是在发票后在irb中输出i.payment_状态。查找。或者只返回@@payment\u status\u数据而不是@@payment\u status\u数据[payment\u status],以查看哈希设置是否正确。
class MyModel < ActiveRecord
  @@my_select_something_data = {"key1" => "text 1", "key2" => "text2"}
  def text_for_something_selectable
    @@my_select_something_data[field_that_stores_id]
  end
end
@my_object.text_for_something_selectable
class ReturnHeader < AciveRecord::Base
  @@return_reason_keys = {"010" => "Wrong Produc",
                          "DAM" => "Damaged",
                          "AMT" => "Wrong Amount"}

  def self.return_reason_select
    @@return_reason_keys.invert
  end

  def return_reason
    @@return_reason_keys[nav_return_reason_code]
  end
end
<%= form_for @return_header do |form| %>
  <%= form.label :nav_return_reason_code "Return Reason" %>
  <%= form.select :nav_return_reason_code, options_for_select(ReturnHeader.return_reason_select, @return_header.nav_return_reason_code) %>
<% end %>
@return_headers.each do |rh|
    rh.return_reason
end