本文介绍了rails 4 中不允许的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关 collection_check_boxes 的内容,但我不明白如何设置选中的值.我有以下模型:

I read about collection_check_boxes but I don't understand how can I set the checked values.I have the following model:

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=objective_ids 但没有任何反应.

the html checkbox are ok but I don't know how to set the values to objective. I was tried define objective_ids= objectives_ids but nothing happens.

在控制器中:

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 => [])

这篇关于rails 4 中不允许的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:38