How to convert param to hash with Rails controller (updated from time to time)

Because params [: some_key] is String or ActionController :: Parameters, it's complicated.


class MyController < ApplicatinoController
  def my_action
    #
    hash_of(params[:some_key]) #call
    #
    head: :no_content
  end

  private
  def hash_of(param)
    case param
    when Hash              #I haven't checked if there really is a hash.
      param
    when String
      JSON.parse(param)    #Throw an error if it's not a JSON string
    when ApplicationController::Parameters
      param.to_unsafe_h
    end
  end
end

reference

Recommended Posts

How to convert param to hash with Rails controller (updated from time to time)
[Rails] How to convert from erb to haml
[Rails] How to convert UC time display to Japanese time display
How to compare only the time with Rails (from what time to what time, something like)
[Rails] How to read the XML file uploaded from the screen with Hash type
[Updated from time to time] Ruby on Rails Convenient methods
How to get along with Rails
Memorandum Poem (updated from time to time)
[Rails] How to use rails console with docker
Convert from java UTC time to JST time
[Rails] Assigning variables from controller to JavaScript
How to build Rails 6 environment with Docker
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Reverse lookup] Spring Security (updated from time to time)
How to link Rails6 Vue (from environment construction)
SpringBoot useful site collection (updated from time to time)
[Updated from time to time] Links that are indebted
How to separate .scss by controller in Rails
I want to play with Firestore from Rails
[Rails] How to easily implement numbers with pull-down
[Rails] How to build an environment with Docker
How to make batch processing with Rails + Heroku configuration
[Rails] How to search by multiple values ​​with LIKE
How to write Rails
How to push an app developed with Rails to Github
How to delete a new_record object built with Rails
How to make an almost static page with rails
How to manually generate a JWT with Rails Knock
How to output standard from an array with forEach
How to uninstall Rails
[Rails] How to deal with URL changes after render
[Eclipse] Summary of environment settings * Updated from time to time
[Rails] How to edit and customize devise view and controller
How to get started with creating a Rails app
[Docker + Rails] How to deal with Rails server startup failure
How to enable submit button every time with jquery
How to get jdk etc from oracle with cli
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
How to automatically generate ER diagram when migrating with Rails6
Let's write how to make API with SpringBoot + Docker from 0
How to write a migration from Rails datetime type to date type
How to get values in real time with TextWatcher (Android)
[Java] How to convert a character string from String type to byte type
[With back tricks] How to introduce React to the simplest Rails
How to specify db when creating an app with rails
Personally recommended Intellij IDEA initial settings (updated from time to time)
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
Convert from ○ months to ○ years ○ months
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"