在Rails中使用simple_form时,如何仅使用它来显示某些字段的数据而不是输入?

cmr*_*rds 4 ruby-on-rails simple-form

我正在使用simple_form创建我的表单,除了我想显示一些文本而不是显示某种类型的输入框之外,它都很好.所以我需要显示一个标签以及显示文本,例如Name:Chris,其中"Name"是标签,"Chris"是显示文本.

所以想象一下我有一个simple_form:

=simple_form_for @property do |f|
  =f.display_field "Contact Name",  "Chris"
  =f.input :customer_reference
  =f.input :premises_description
  =f.input :po_number, :label=>"Purchase Order Number"
Run Code Online (Sandbox Code Playgroud)

"f.display_field"是一个组合方法,但我想象的方法看起来是我需要的.所有它会做的是显示标签和旁边的一些文字.实现这一目标的最简单方法是什么?

干杯克里斯

Sim*_*tsa 11

我为此目的使用自定义输入:

class FakeInput < SimpleForm::Inputs::Base
  # This method usually returns input's html like <input ... />
  # but in this case it returns just a value of the attribute.
  def input
    @builder.object.send(attribute_name)
  end
end
Run Code Online (Sandbox Code Playgroud)

如果您将它放在app/inputs/fake_input.rb中的某个位置,您将能够以简单的形式使用它:

= simple_form_for @property do |f|
  = f.input :contact_name, :as => :fake
Run Code Online (Sandbox Code Playgroud)

输入的类型来自输入的类名(没有"输入",下划线).所以对于FakeInput来说,它是:假的.