我读过collection_check_boxes
但我不明白如何设置检查值。我有以下模型:
class Objective < ActiveRecord::Base
has_many :indicators
has_many :objective_children, class_name: "Objective", foreign_key: "parent_id"
def objective_ids
objective_children.collect{|o| o.id}
end
def objective_ids= objectives_ids
objectives_ids.each do |id|
objective_children << Objective.find(id)
end
end
end
编辑视图:
<%= form_for(@objective) do |f| %>
<%= f.collection_check_boxes :objective_ids, Objective.all, :id, :name %>
<%= f.submit %>
<% end %>
html 复选框没问题,但我不知道如何将值设置为objective
. 我被尝试定义objective_ids= objectives_ids
但没有任何反应。
在控制器中:
class ObjectivesController < ApplicationController
def objective_params
params.require(:objective).permit(:name, :code, :description, :objective_ids)
end
end
编辑
日志文件说Unpermitted parameters: perspective_id, objective_ids