14

我读过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

4

2 回答 2

28

我解决了换线

params.require(:objective).permit(:name, :code, :description, :objective_ids)

params.require(:objective).permit(:name, :code, :description, :objective_ids => [])
于 2013-08-01T01:26:41.293 回答
3

我遇到了同样的问题,试试这条线:

params.require(:objective).permit(:name, :code, :description, :objective_ids => [])

但不起作用,我改为:

params.require(:objective).permit(:name, :code, :description, {:objective_ids => []})

它有效!

于 2014-05-03T14:35:04.007 回答