如何在Rails 4中使用强参数?

时间:2013-12-02 14:41:55

标签: ruby-on-rails ruby-on-rails-4 strong-parameters

我正在尝试将我的应用程序从Rails 3升级到Rails 4,我似乎无法在第12行获得语法:

class ProjectsController < ApplicationController

  before_filter :find_project

  ...

  private

  def find_project
    @project = Project.find(params[:id])
  end

  def valid_people
    if params[:project][:person_ids].present? # how to do this with strong parameters?
      person_ids = params[:project][:person_ids].map(&:to_i)
      valid_ids = current_user.people.pluck(:id)
      redirect_to root_path if (person_ids - valid_ids).any?
    end
  end

  def project_params
    params.require(:project).permit(:name, :description, :person_ids)
  end

end

我一直收到此错误:Unpermitted parameters: person_ids

如何仅使用person_ids参数?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

强参数的想法是您调用已定义的函数来获取参数

if params[:project][:person_ids].present? # how to do this with strong parameters?

会变成

if project_params[:person_ids].present? # how to do this with strong parameters?

我也猜测:person_ids是一个数组,如果是这样,请将:person_ids替换为{:person_ids => []}