Created
October 3, 2018 04:41
-
-
Save terryyounghk/bcc3b705c642b2ed7e407fc42d5a7219 to your computer and use it in GitHub Desktop.
params vs. params.permit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountController < ApplicationController | |
# Just to disable the CSRF protection for specific controller for quick testing | |
# @see https://stackoverflow.com/a/34252150/277666 | |
skip_before_action :verify_authenticity_token | |
def test | |
# 'params' is NOT recommended to be used directly | |
# Imagine in addition to 'id' and 'name', a parameter 'balance' is passed in | |
# with the value of 0 (zero), and 'params' is directly used to update a table. | |
# rendor json: params | |
# 'params' should at least be filtered using params.permit | |
# @see https://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit | |
render json: create_params | |
end | |
def create_params | |
params.permit( | |
:id, | |
:name | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
post '/account', to: 'account#test' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment