I have a controller that uses strong params with require
and permit
. When I'm testing this endpoint with postman, I keep getting something like:
undefined method `permit' for "{foo: [1,2,3], bar: 1}":String
I know this controller works fine when I make the post request through the UI, but testing with postman gives me this error each time. I'm sending my payload through raw as JSON(application/json) and the header with the corresponding content type too.
This is the body I'm passing thru:
{
"foos": {"foo":[1,2,3], "bar": 2675}
}
def foos_params
params.require(:foos).permit(:foo, :bar)
end
In my opinion,
with Postman input params:
key : value
foos[foo] : [1,2,3] # this is string: JSON.stringify([1,2,3]) => "[1,2,3]"
foos[bar] : 2675
In controller:
def foos_params
params.require(:foos).permit(:foo, :bar)
end
But, we need convert string to array for :foo
:
ActiveSupport::JSON.decode(foos_params[:foo]) # => [1, 2, 3]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.