【问题标题】:simple_form no input found for jsonsimple_form 找不到 json 的输入
【发布时间】:2014-04-14 13:07:04
【问题描述】:

环境 Mac OS X 导轨 4.0.4 红宝石 2.1.1

宝石: 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 类型,我真的不知道如何解决这个问题(simple_form 的新手)。

希望你能帮忙

【问题讨论】:

    标签: ruby-on-rails json postgresql simple-form


    【解决方案1】:

    simple_form,本机不支持JSON 类型。但事实证明,JSON 是基于文本的数据格式,您可以简单地使用 textarea 来输出 json。

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

    这应该可以正常工作

    【讨论】:

      【解决方案2】:

      别忘了逗号,&lt;%= f.input, as: :text %&gt;

      【讨论】:

        【解决方案3】:

        您还可以在初始化程序中放入以下行来定义自定义输入映射。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多