【问题标题】:Wrong type being used when using simple_form with reform将 simple_form 与改革一起使用时使用了错误的类型
【发布时间】:2016-02-13 00:57:04
【问题描述】:

给定以下表格

class EntryForm < Reform::Form
  property :composition
  property :native_language_version

  validates :composition, presence: true
end

以及以下架构

  create_table "entries", force: :cascade do |t|
    t.text     "composition"
    t.text     "native_language_version"
    t.integer  "language_id"
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
  end

以及下面的控制器代码

class EntriesController < ApplicationController
  def new
    @entry = EntryForm.new(Entry.new)
    # @entry = Entry.new
  end
end

以及simple_form的以下代码

= simple_form_for(@entry) do |f|
  = f.input :composition
  = f.input :native_language_version
  = f.submit

我得到的不是compositionnative_language_versiontextarea,而是

<input class="string required form-control" type="text" name="entry[composition]" id="entry_composition">

改用 @entry = Entry.new 给了我一个 textarea 元素,这正是我想要的:

<textarea class="text optional form-control" name="entry[composition]" id="entry_composition"></textarea>

我尝试将type: :text 添加到EntryForm 中的:composition 属性,但没有帮助。

我也知道,我可以指定实际的输入类型,而不是使用 f.input,但这是一种 hack。

我如何将compositiontext 而不是stringEntryForm 的事实传递给simple_form?

我正在使用 Rails 4.2.5.1、simple_form 3.2.1 和改革 2.1.0。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 simple-form reform


    【解决方案1】:

    你不能。当模型被 Reform::Form 包裹时,你必须明确告诉 SimpleForm 你想要一个文本区域。

    = f.input :composition, as: :text_area

    原因是在确定列数据库类型 SimpleForm relies on a part of ActiveRecord interface 时,Reform::Form 没有提供。

    【讨论】:

      猜你喜欢
      • 2022-07-25
      • 2019-11-03
      • 1970-01-01
      • 2016-08-28
      • 2013-02-15
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多