从simple_form布局指定可见输入字段

时间:2017-12-06 12:33:23

标签: ruby-on-rails simple-form slim-lang

我正在加载两个呈现simple_form表单布局的视图,新建和编辑。

我需要在最初创建用户配置文件时显示所有输入字段(新),并指定在用于编辑时不显示的字段。我已经在persisted周围看到了很多信息,但无法弄清楚。

我的_form.html.slim文件。

= simple_form_for([:admin, User.new]) do |f|
  = f.error_notification
  .form-inputs
    = f.input :name, required: true, label: 'Name'
    = f.input :email, required: true
    = f.input :password, required: true, placeholder: ("#{@minimum_password_length} characters minimum" if @minimum_password_length)
    = f.input :password_confirmation, required: true

  .form-actions
    = f.button :submit, "Create User", class: 'btn btn-primary btn-block btn-lg'

我使用Rails和Slim - 感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您确实可以使用persisted?有条件地呈现表单中的字段。

= simple_form_for([:admin, User.new]) do |f|
  = f.error_notification
  .form-inputs
    = f.input :name, required: true, label: 'Name'
    = f.input :email, required: true

    -# Say you don't want user to edit his password after creation
    - unless f.object.persisted?
      = f.input :password, required: true, placeholder: ("#{@minimum_password_length} characters minimum" if @minimum_password_length)
      = f.input :password_confirmation, required: true

    .form-actions
      = f.button :submit, "Create User", class: 'btn btn-primary btn-block btn-lg'

此处f.object将是要为其呈现表单的User实例。