I need to store the bellow parameter into a table:
[{"id":"1","name":"Cheese and red deans","amount":2,"price":"0.65"},{"id":"2","name":"Pork, cheese and red beans","amount":2,"price":"0.85"},{"id":"3","name":"Soda","amount":1,"price":"0.65"}]
The controller's code is this (I'm using ActiveController):
#POST /ordertemps
def create
@ordertemp = Ordertemp.new(ordertemp_params)
if @ordertemp.save
render json: @ordertemp
else
render error: { error: 'Unable to create an order'}, status: 400
end
end
However, I'm getting an error in the next code:
private
def ordertemp_params
params.require(:ordertemp).permit(:name, :amount, :price)
end
I found the method permit is just related to hashes, so apparently my data is not in that format, so I tried to use different functions such as: .each_with_index.to_a, to_h, Hash but nothing seems to work.
So my questions are:
- what function should I use to convert my data to a hash?
- Do I need to iterate my data in order to save each item into the table?
Thanks a lot
:idto permit. If you need to process multiple params at once I think you'll have to adapt your controller code.def create @ordertemp = ordertemp_params.map { |attrs| Ordertemp.new(attrs) } if @ordertemp.map(&:save).all?(true) render json: @ordertemp, status: :created, location: @ordertemp else render json: @ordertemp.map(&:errors), status: :unprocessable_entity end end; def ordertemp_params params.require(:ordertemp).map { |param| param.permit(:id, :name, :amount, :price) } end