自定义form_for字段可在模型中访问

时间:2015-06-28 23:58:14

标签: ruby-on-rails ruby model attributes

我在form_for

中有自定义字段
check_box_tag "files_to_delete[]", :attach.id

允许参数:

params.require(:post).permit( ... , :files_to_delete => [])

我的模型中有before_action :some_method,我希望能够访问files_to_delete

def some_method
  files_to_delete.each do |attach|
    attach.clear
  end
end

但我明白了:

undefined local variable or method `files_to_delete' for #<Post:0x007f5c4cb51ad0>

1 个答案:

答案 0 :(得分:1)

您的模型需要files_to_delete的setter和getter。将以下行添加到模型中:

attr_accessor :files_to_delete