简体   繁体   English

轨道4中未允许的参数

[英]Unpermitted parameters in rails 4

I read about collection_check_boxes but I don't understand how can I set the checked values. 我读到了关于collection_check_boxes但我不明白如何设置选中的值。 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

edit view: 编辑视图:

<%= form_for(@objective) do |f| %>
  <%= f.collection_check_boxes :objective_ids, Objective.all, :id, :name %>
  <%= f.submit %>
<% end %>

the html checkbox are ok but I don't know how to set the values to objective . html复选框没问题,但我不知道如何将值设置为objective I was tried define objective_ids= objectives_ids but nothing happens. 我试过定义objective_ids= objectives_ids但没有任何反应。

In Controller: 在控制器中:

class ObjectivesController < ApplicationController
    def objective_params
      params.require(:objective).permit(:name, :code, :description, :objective_ids)
    end
end

EDIT The log file says Unpermitted parameters: perspective_id, objective_ids 编辑日志文件显示未Unpermitted parameters: perspective_id, objective_ids

I solved changing the line 我解决了换线问题

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

to

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

I get the same problem, try this line: 我遇到同样的问题,试试这行:

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

but does not work and i change to: 但不起作用,我改为:

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

and it works !! 它的工作原理!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM