Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you have to use fetch for Rails params?

In the docs, they have:

params = ActionController::Parameters.new(person: { name: "Francesco" })

and then use params.fetch(:person) to get person, but can't you just do params[:person]? to get the same data?

like image 282
stackjlei Avatar asked Dec 09 '25 12:12

stackjlei


1 Answers

I suppose it depends on the behavior you desire.

params = ActionController::Parameters.new
params.fetch(:person)

Will raise an error. As reflected in the docs, you can specify further behavior with fetch, if you so desire.

params = ActionController::Parameters.new
params[:person]

Will return nil.

like image 155
jvillian Avatar answered Dec 12 '25 03:12

jvillian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!