Understanding Parameters
Parameters are the data sent with incoming requests in a Rails application. They are accessible via the params
hash, which is an instance of ActionController::Parameters
. Unlike a standard Ruby hash, params
treats both symbol (:key
) and string ("key"
) keys as equivalent.
Rails supports several types of parameters:
- Path Parameters: Encoded in the URL, e.g.,
/articles/:id
, whereid
is a path parameter. - Query String Parameters: Appended to the URL, e.g.,
/articles?category=tech
. - Form Data: Submitted via
POST
requests when a user submits a form. - JSON Data: Commonly used in API requests where JSON is sent in the request body.
Example Usage:
# A request to /articles/5
params[:id] # => "5"
# A request to /articles?category=tech
params[:category] # => "tech"
Secure Handling with Strong Parameters
Strong parameters allow explicit permission of specific attributes before saving them to the database. This prevents mass assignment vulnerabilities.
Example Without Strong Parameters:
class ArticlesController < ApplicationController…