我有一个Availability
模型,我想发送一个API发布请求以一次创建多个可用性。
我偶然发现了这个post on medium,我很激动,它可以解决我的问题,但是没有解决,因为在我通过API发送请求时,作者使用的是Rails表单模板。
我对documentation进行了进一步研究,发现了一种创建多个记录的方法,在那里也尝试了该方法,但是对我不起作用。
这是我的控制器代码。 create
方法和availability_params
方法。
module API
module V1
class AvailabilitiesController < ApiController
def create
availability = Availability.new(availability_params)
availability.tutor = current_user
availability.start_time = DateTime.parse("#{availability_params[:day_of_the_week]} #{availability_params[:start_time]}")
availability.end_time = DateTime.parse("#{availability_params[:day_of_the_week]} #{availability_params[:end_time]}")
if availability.start_time < DateTime.current && availability.end_time < DateTime.current
availability.start_time += 1.week
availability.end_time += 1.week
end
if availability.save
render json: {
availability: availability,
message: 'Your availability was created successfully.'
}
else
render json: { message: 'There was a problem creating your availability.', errors: availability.errors }, status: :bad_request
end
end
def destroy
Availability.where(id: params[:ids]).destroy_all
end
def availability_params
params.require(:availability).permit(:day_of_the_week, :start_time, :end_time).tap do |availability_params|
availability_params.require([:day_of_the_week, :start_time, :end_time])
end
end
end
end
end
使用axios从前端发出的请求如下所示:
axios.post('/api/v1/availabilities', { availability: payload })
destroy函数还会发送这种类型的请求:
axios.delete('/api/v1/availabilities', { params: { ids: payload } })
该请求的示例输出在发送后如下所示:
web_1 | Started POST "/api/v1/availabilities" for 172.18.0.1 at 2018-10-29 18:07:27 +0000
web_1 | Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1 | Processing by API::V1::AvailabilitiesController#create as JSON
web_1 | Parameters: {"availability"=>[{"start_time"=>"2018-10-30T13:00:00.000Z", "end_time"=>"2018-10-30T13:30:00.000Z", "day_of_the_week"=>"Tuesday", "tutor_id"=>1}, {"start_time"=>"2018-10-30T13:30:00.000Z", "end_time"=>"2018-10-30T14:00:00.000Z", "day_of_the_week"=>"Tuesday", "tutor_id"=>1}, {"start_time"=>"2018-10-30T14:00:00.000Z", "end_time"=>"2018-10-30T14:30:00.000Z", "day_of_the_week"=>"Tuesday", "tutor_id"=>1}, {"start_time"=>"2018-10-30T14:30:00.000Z", "end_time"=>"2018-10-30T15:00:00.000Z", "day_of_the_week"=>"Tuesday", "tutor_id"=>1}, {"start_time"=>"2018-10-30T15:00:00.000Z", "end_time"=>"2018-10-30T15:30:00.000Z", "day_of_the_week"=>"Tuesday", "tutor_id"=>1}]}
web_1 | User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY id LIMIT $2 [["id", 1], ["LIMIT", 1]]
web_1 | ↳ app/controllers/application_controller.rb:114
web_1 | Completed 500 Internal Server Error in 35ms (ActiveRecord: 1.0ms)
web_1 |
web_1 |
web_1 |
web_1 | NoMethodError (undefined method `permit' for #<Array:0x00007f1f26d889d8>):
我到底在做什么错?任何建议将不胜感激,谢谢。
答案 0 :(得分:0)
您已经关闭。如果查看日志,则可以看到params [:availability]是一个数组,但是您的robot.respond(!(/command1/, /command2/, /command3/, etc.) function (msg) {
msg.reply('Returned message')
})
方法将其作为代表一个项目的属性的简单哈希值进行处理。
像这样循环播放:
create