What we want to achieve
I am sending an array from Nuxt.js, Rails is in API mode and I am getting an error when I try to retrieve and save the array contents one by one. I want to save the array received by params and add multiple records.
Error
ArgumentError (When assigning attributes, you must pass a hash as an argument, Array passed.):
Code
Controller
def create
schedule = Schedule.new(post_params)
if schedule.save
render json: schedule, status: :created
else
render json: schedule, status: :internal_server_error
end
end
def post_params
params.require(:post).map do |schedule|
schedule.permit(:id, :name, :start, :end, :color, :timed, :long_time, :post_id, :post_item_id)
end
end
parameter
post: [,…]
0: {name: "", color: "#2196F3", start: 1648275300000, end: 1648276200000, timed: true, long_time: true}
1: {name: "", color: "#2196F3", start: 1648361700000, end: 1648362600000, timed: true, long_time: true}
2: {name: "", color: "#2196F3", start: 1648448100000, end: 1648449000000, timed: true, long_time: true}
3: {name: "", color: "#2196F3", start: 1648534500000, end: 1648535400000, timed: true, long_time: true}
4: {name: "", color: "#2196F3", start: 1648620900000, end: 1648621800000, timed: true, long_time: true}
What we tried
・I tried to retrieve a value using map.
・I tried to convert it to a hash using to_h at the end of pemit, but neither the value nor the error changed.
CodePudding user response:
Well first question here is what is happening if one of the records failed, do previously created should be rollbacked and following actions to be aborted or you just ignore invalid creations.
In case of rollback/aborting you can go for Schedule.create!(post_params)
- responding with the proper message error and all others will be rollbacked.
In case of ignoring failure ones, go for Schedule.create!(post_params)
- responding with created and failure ones.