简体   繁体   中英

How to use strong parameters in Rails 4?

I am trying to upgrade my app from Rails 3 to Rails 4 and I can't seem to get the syntax right in line 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

I keep getting this error: Unpermitted parameters: person_ids

How can I use only the person_ids parameter?

Thanks for any help.

The idea of strong parameters is that you call the function you have defined to get your parameters

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

would become

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

also i'm guessing :person_ids is an array if so replace your :person_ids with {:person_ids => []}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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