class StrongParametersDsl::Railtie
Public Class Methods
strong_params(name, options = {}, &block)
click to toggle source
Examples
¶ ↑
class UsersController < ActionController::Base strong_params :user do permit :name, :email, comments_attributes: [:comment, :post_id] end ... or ... strong_params :user, permit: [name, :email, comments_attributes: [:comment, :post_id]] # you can also declare a key whose value is a hash with arbitrary keys strong_params :activity, any: [:api_log], permit: [:event, :timestamp, :ip_address] def create # the named param method is created based on the argument passed # to the strong_params call User.create user_params ... rest of code ... end end
# File lib/strong_parameters_dsl.rb, line 30 def self.strong_params(name, options = {}, &block) param_method_name = "#{name}_params".to_sym define_method param_method_name do params.require(name).tap do |strongified| # use a block to assign permitted keys strongified.instance_exec &block if block_given? # mix and match using hash options to assign permitted values if options[:permit].present? strongified.permit *Array(options[:permit]) end # shortcut to adding a key whose value is a hash with arbitrary keys if options[:any].present? Array(options[:any]).map do |key| strongified[key] = params[name][key] end end end end private param_method_name end