It is a memorandum.
params.require(:Model name).permit(:Column name).to_s.include?("Search word")
By writing to_s, the data contained in the "permit (: column name)" column will be treated as a character string.
Example ①
controller.rb
def update
if params.require(:fridge).permit(:icecream).to_s.include?("Gari-gari")
@fridge.update(fridge_params)
redirect_to root_path
else
render :edit
end
end
def fridge_params
params.require(:fridge).permit(:vegetable, :meat, :icecream).merge(user_id: current_user.id)
end
params
Parameters: {"authenticity_token"=>"XXX==", "fridge"=>{"vegetable"=>"eggplant", "meat"=>"beef", "icecream"=>"Gari-gari"}
Example ②
controller.rb
def update
if params.require(:fridge).permit(:icecream).to_s.include?("Yukimi Daifuku")
@fridge.update(fridge_params)
redirect_to root_path
else
render :edit
end
end
# fridge_params is omitted hereafter
params
Parameters: {"authenticity_token"=>"XXX==", "fridge"=>{"vegetable"=>"green pepper", "meat"=>"pork", "icecream"=>"Yukimi Daifuku"}
Example ③
controller.rb
def update
if params.require(:fridge).permit(:meat).to_s.include?("chicken")
@fridge.update(fridge_params)
redirect_to root_path
else
render :edit
end
end
params
Parameters: {"authenticity_token"=>"XXX==", "fridge"=>{"vegetable"=>"Tomato", "meat"=>"Pork and chicken", "icecream"=>"jumbo"}
Example ①
controller.rb
def update
if params.require(:fridge).permit(:vegetable).to_s.include?("Bean sprouts")
@fridge.update(fridge_params)
redirect_to root_path
else
render :edit
end
end
params
Parameters: {"authenticity_token"=>"XXX==", "fridge"=>{"vegetable"=>"Chinese chive", "meat"=>"beef", "icecream"=>"Refreshing"}
Example ②
controller.rb
def update
if params.require(:fridge).permit(:vegetable).to_s.include?("Broccoli")
@fridge.update(fridge_params)
redirect_to root_path
else
render :edit
end
end
params
Parameters: {"authenticity_token"=>"XXX==", "fridge"=>{"vegetable"=>"broccoli", "meat"=>"Mutton", "icecream"=>"Refreshing"}
Example ③
controller.rb
def update
if params.require(:fridge).permit(:icecream).to_s.include?("Giant corn")
@fridge.update(fridge_params)
redirect_to root_path
else
render :edit
end
end
params
Parameters: {"authenticity_token"=>"XXX==", "fridge"=>{"vegetable"=>"Potatoes", "meat"=>"Human flesh", "icecream"=>"Gian and corn"}
Just limiting the character strings you want to save with the controller does not mean that it will play the save in the DB. You may need to be careful when using it.
https://stackoverflow.com/questions/57625775/how-to-check-if-parameters-string-contains-a-substring https://stackoverflow.com/questions/5629402/how-to-test-if-parameters-exist-in-rails/5629506
Recommended Posts