2014-04-14 30 views
5

環境 的Mac OS X 的Rails 4.0.4 的Ruby 2.1.1輸入simple_form沒有發現JSON

寶石: simple_form

創建一個JSON類型的字段,在這裏我的移民文件的模型:

create_table :my_model do |t| 
    t.string :name 
    t.json :my_field 

    t.timestamps 
end 

,現在當我嘗試創建一個新的對象(新的形式),我收到此錯誤

RuntimeError - No input found for json: 
    simple_form (3.0.2) lib/simple_form/form_builder.rb:551:in `find_mapping' 
    simple_form (3.0.2) lib/simple_form/form_builder.rb:482:in `find_input' 
    simple_form (3.0.2) lib/simple_form/form_builder.rb:111:in `input' 

我猜測simple_form沒有很好的處理JSON類型,我真的不知道如何解決這個問題(new_ie with simple_form)。

希望你能幫助

回答

7

simple_form,不支持JSON類型本身。但事實證明,JSON是基於文本的數據格式,您可以簡單地使用textarea輸出json。

<%= f.my_field, as: :text %> 

這應該做工精細

1

不要忘了逗號,<%= f.input, as: :text %>

+0

這並沒有提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/16880487) – the4kman

0

你也可以把下面一行在初始化定義自定義輸入映射。

SimpleForm::FormBuilder.map_type :json, to: SimpleForm::Inputs::TextInput