Unpermitted parameters in rails 4

谁都会走 提交于 2019-11-29 17:17:35

问题


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

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. I was tried define objective_ids= objectives_ids but nothing happens.

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


回答1:


I solved changing the line

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

to

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



回答2:


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 !!



来源:https://stackoverflow.com/questions/17982904/unpermitted-parameters-in-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!