在rails admin中,创建一个所需的文本字段并为其指定最大长度

ear*_*old 3 ruby-on-rails rails-admin

我觉得这应该是显而易见的,但我在文档中找不到它.

我正在使用rails_admin构建一个简单的CMS.

考虑一下:

 config.model Article do
    list do
      field :title
      field :created_at
      field :updated_at
    end
    edit do
      field :title
      field :description do
        required true #this line is pseudo code! What is the real thing?
        maxlength 600 #ditto this line
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

如何将这两行伪代码转换为"required"和"maxlength"的实际标记?

Mun*_*sim 6

为了获得所需的输出,您的配置应该是:

config.model Article do
  list do
    field :title
    field :created_at
    field :updated_at
  end
  edit do
    field :title
    field :description, :string do  #use second parameter to set field type
      required true #this will just set a hints text
      #to set max length use:
      html_attributes do
       {:maxlength => 600} #dont use 600 as maxlength for a string field. It will break the UI
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 你接近工作,但我最终做了这样的`字段:description do required(true)help"必需 - 最大长度为600个字符"结束`在字段上进行一些后端验证以添加错误消息. (2认同)