2016-02-13 18 views
1

給出的以下形式錯誤類型使用simple_form當與改革

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 

和下面的代碼被用於for simple_form

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

,而不是得到了composition一個textareanative_language_version,我得到

<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我可以指定實際的輸入類型,但這是一種破解。

如何將compositiontext而不是stringEntryForm這一事實傳遞給simple_form?

我使用的是Rails 4.2.5.1,simple_form 3.2.1和2.1.0。

回答

2

你不能。當模型被Reform::Form包裝時,您必須明確告訴SimpleForm您需要textarea。

= f.input :composition, as: :text_area

其原因是,確定列的數據庫類型SimpleForm relies on a part of ActiveRecord interface時改革::表格不提供。