[Ruby On Rails] How to search the contents of params using include?

It is a memorandum.

retrieval method

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 when update is possible

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 when update is not possible

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"}

important point

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.

Reference article

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

[Ruby On Rails] How to search the contents of params using include?
[Ruby On Rails] How to search and save the data of the parent table from the child table
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby on Rails] How to change the column name
[Rails] How to get the contents of strong parameters
Try using the query attribute of Ruby on Rails
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to use Ruby on Rails
[Ruby] How to retrieve the contents of a double hash
[Ruby on Rails] How to use CarrierWave
part of the syntax of ruby ​​on rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
How to find the cause of the Ruby error
[Ruby on Rails] How to display error messages
Customize how to divide the contents of Recyclerview
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to debug the processing in the Ruby on Rails model only on the console
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
How to resolve errors that occur in the "Ruby on Rails" integration test
How to implement image posting function using Active Storage in Ruby on Rails
[Ruby on Rails] How to write enum in Japanese
Docker the development environment of Ruby on Rails project
[Ruby] How to find the sum of each digit
[Rails] How to change the column name of the table
[Ruby On Rails] How to reset DB in Heroku
Rails learning How to implement search function using ActiveModel
(Ruby on Rails6) How to create models and tables
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
A note about the seed function of Ruby on Rails
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
Let's summarize how to extend the expiration date of Rails
How to install JDK 8 on Windows without using the installer
[Rails] How to display the list of posts by category
How to run React and Rails on the same server
How to check the database of apps deployed on Heroku
How to deploy jQuery on Rails
How to use the include? method
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
How to deploy Bootstrap on Rails
Rails on Tiles (how to write)
[Ruby] Display the contents of variables
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Rails] How to get the URL of the transition source and redirect
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails] How to omit the display of the character string of the link_to method