Skip to content

Instantly share code, notes, and snippets.

@ankit8898
Created July 30, 2013 11:54
Show Gist options
  • Save ankit8898/6112306 to your computer and use it in GitHub Desktop.
Save ankit8898/6112306 to your computer and use it in GitHub Desktop.
Strong parametes
class ApplicationController < ActionController::Base
helper_method :permitted_params
private
def permitted_params
PermittedParams.new(params)
end
+++++++++++++++raw+++++{"utf8"=>"✓", "authenticity_token"=>"CfGubZxXPW/7XEHEhnFTjKVKByNxGb5r3KJqoqoXHms=", "unit_plate"=>{"name"=>"Okyqaa", "name_type"=>"boyqaa", "parent_id"=>"", "unit_plate_components_attributes"=>{"1375184158151"=>{"component_id"=>"115", "_destroy"=>"false"}}}, "commit"=>"Create Unit plate", "action"=>"create", "controller"=>"admin/unit_plates"}
Unpermitted parameters: component_id, _destroy
Unpermitted parameters: name_type
++++++++++++++++++++{"name"=>"Okyqaa", "parent_id"=>"", "unit_plate_components_attributes"=>{"1375184158151"=>{}}}
Klass = Struct.new(:params) unless defined?(Klass
class PermittedParams < Klass
#For: admin/unit_plates/new
def unit_plate
params.require(:unit_plate).permit(:name,:name_type,:type,:unit_plate_components_attributes => [:id, '_destroy',:component_id,:name,:type,:parent_id,:lft,:rgt,:depth])
end
end
end
def create
Rails.logger.info "+++++++++++++++raw+++++#{params.inspect}"
Rails.logger.info "++++++++++++++++++++#{permitted_params.unit_plate.inspect}"
@unit_plate = UnitPlate.new(permitted_params.unit_plate)
if @unit_plate.save
redirect_to edit_admin_unit_plate_url(@unit_plate), :notice => 'Template was successfully created.'
else
render :action => 'new'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment