Rails Cocoon只读字段

时间:2017-08-13 17:25:05

标签: ruby-on-rails cocoon-gem

我正在使用Cocoon来嵌套表单。对于已创建的LinkedList<Integer> list = ... // Inclusive lower bound int lowerBound = ... // Exclusive upper bound int upperBound = ... ListIterator<Integer> listIter = list.listIterator(); while (listIter.hasNext()) { int value = listIter.next(); // Check if the value is inside bounds if (value >= lowerBound || value < upperBound) { // Remove the element from the list using the iterator // which prevents ConcurrentModificationException listIter.remove(); } } 条记录 ,我希望task为read_only。

项目/ _form ::

description

和_task_fields:

= simple_form_for @project do |f|
  = f.input :name
  = f.input :description
  %h3 Tasks
  #tasks
    = f.simple_fields_for :tasks do |task|
      = render 'task_fields', f: task
    .links
      = link_to_add_association 'add task', f, :tasks
  = f.submit

目前.nested-fields - if @task.description? = f.text_field :description, disabled: true - else = f.input :description = f.input :done, as: :boolean = link_to_remove_association "remove task", f 中的验证无效:_task_fieldsundefined method <description> for nil:NilClass语句一致。如何正确编写if

1 个答案:

答案 0 :(得分:1)

  

对于已经创建的任务记录,我想要描述   READ_ONLY。

你做错了。您应该可以使用new_record?这样进行检查

.nested-fields
  - unless f.object.new_record?
    = f.text_field :description, disabled: true
  - else
    = f.input :description
    = f.input :done, as: :boolean
  = link_to_remove_association "remove task", f

此外,通过使用disabled: true,不会提交说明的值,也无法通过params传递。如果您想在params中包含说明值,请改用readonly: true

.nested-fields
  - unless f.object.new_record?
    = f.text_field :description, readonly: true
  - else
    = f.input :description
    = f.input :done, as: :boolean
  = link_to_remove_association "remove task", f