我读过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
我解决了更改行的问题
params.require(:objective).permit(:name, :code, :description, :objective_ids)
至
params.require(:objective).permit(:name, :code, :description, :objective_ids => [])
我遇到同样的问题,试试这行:
params.require(:objective).permit(:name, :code, :description, :objective_ids => [])
但不起作用,我改为:
params.require(:objective).permit(:name, :code, :description, {:objective_ids => []})
它起作用了!!