ruby-on-railsruby-on-rails-4simple-formreform

Wrong type being used when using simple_form with reform


Given the following form

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

  validates :composition, presence: true
end

and the following schema

  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

and the following controller code

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

and the following code for simple_form

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

rather than getting a textarea for composition and native_language_version, I get

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

changing to using @entry = Entry.new gives me a textarea element instead, which is what I want:

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

I tried adding type: :text to the :composition property in EntryForm, but it didn't help.

I also know that rather than using f.input I could specify the actual input type, but that's a hack.

How do I pass the fact that composition is a text rather than a string through EntryForm to simple_form?

I'm using Rails 4.2.5.1, simple_form 3.2.1, and reform 2.1.0.


Solution

  • You can't. When the model is wrapped by a Reform::Form, you have to explicitly tell SimpleForm that you want a textarea.

    = f.input :composition, as: :text_area

    The reason is that when determining column database type SimpleForm relies on a part of ActiveRecord interface which the Reform::Form doesn't provide.