【问题标题】:Unpermitted parameters in rails 4rails 4中不允许的参数
【发布时间】:2013-08-01 17:13:38
【问题描述】:

我阅读了有关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

【问题讨论】:

    标签: ruby-on-rails checkbox ruby-on-rails-4 view-helpers strong-parameters


    【解决方案1】:

    我解决了换线问题

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

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

    【讨论】:

    • 也可以写成.permit(:name, :code, :description, objective_ids: [])
    • 只要你有 :objective_ids => [] 作为最后一个元素,它就可以工作。如果将它放在其他符号之间,则会出现语法错误。看到语法错误的人,把它放在最后一个元素,错误就会消失。
    【解决方案2】:

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

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

    但不起作用,我改为:

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

    它有效!

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多