Ruby on rails 如何从React发布两个相关Rails模型的请求?
我有一个rails api应用程序作为后端,react应用程序作为前端 在rails内部,我有一个Ruby on rails 如何从React发布两个相关Rails模型的请求?,ruby-on-rails,api,reactjs,associations,Ruby On Rails,Api,Reactjs,Associations,我有一个rails api应用程序作为后端,react应用程序作为前端 在rails内部,我有一个计划模型,它有许多工作者和工作者模型,它属于计划。当用户创建新计划时,他们可以选择一个日期,并选择工人的姓名。我的挣扎是,我想不出一种方法来传递工人的日程安排 以下是我所拥有的: 我有两种取回方法;每个API都向指定的API发送数据/发出POST请求 function postSchedule(date, cb) { return fetch(`api/schedules`, { met
计划
模型,它有许多工作者
和工作者
模型,它属于计划
。当用户创建新计划时,他们可以选择一个日期
,并选择工人的姓名
。我的挣扎是,我想不出一种方法来传递工人的日程安排
以下是我所拥有的:
我有两种取回方法;每个API都向指定的API发送数据/发出POST请求
function postSchedule(date, cb) {
return fetch(`api/schedules`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
date: date,
user_id: 1
})
}).then((response) => response.json())
.then(cb); //cb setStates schedules state in main react app
};
function postWorker(workerName, cb) {
return fetch('api/workers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
worker: workerName,
schedule_id: //how do I know schedule_id?
})
}).then((response) => response.json())
.then(cb); //cb setStates workers state in main react app
}
Rails型号:
//schedule.rb
class Schedule < ApplicationRecord
belongs_to :user
has_many :workers
end
//worker.rb
class Worker < ApplicationRecord
belongs_to :schedule, optional: true
end
表单看起来像这样。用户将同时创建新计划和新工作人员
如果用户正在创建一个新的计划,显然这个计划在DB中还不存在,所以在提交之后我才知道这个计划的ID。当我创建一个新工人时,它需要schedule\u id。我如何让Rails知道这个工人的schedule\u id是什么
将新创建的计划分配给新创建的工作人员的好策略是什么 我的朋友昨晚给了我一个提示,我让它起作用了。他说,除非我提交、接受并回复新的日程表id,否则无法获取日程表id信息
所以为了解决我的问题,我不得不提出两个要求。首先,我需要请求在rails中调度模型,等待rails接受它,然后等待rails返回response
。在响应中是schedule\u id.I。然后用我从rails获得的schedule id分配schedule\u id
这里有点乱,我需要清理一下。但这对我来说很有用:
Client.postSchedule(this.state.date, (schedule) => {
this.setState({schedules: this.state.schedules.concat([schedule])})
}).then(() => Client.postWorker(this.state.worker, this.state.phone, this.state.schedules[this.state.schedules.length - 1].id, (worker) => {
this.setState({workers: this.state.workers.concat([worker])})
})
)
行this.state.schedules[this.state.schedules.length-1].id
有点模糊。它真正做的是,我有一个时间表数组,我在其中存储我的时间表对象。我只是让React知道如何获取数组中的最后一个计划(这将是最新的计划),并为我提供schedules数组中最后一个计划对象的ID
简而言之,(向schedule发送请求)->(等待来自包含我需要的schedule\u id的Rails的响应)->(使用该schedule\u id信息发送另一个请求)
。解决方法是先检查,然后检查。我的朋友昨晚给了我一个提示,我让它开始工作。他说,除非我提交、接受并回复新的日程表id,否则无法获取日程表id信息
所以为了解决我的问题,我不得不提出两个要求。首先,我需要请求在rails中调度模型,等待rails接受它,然后等待rails返回response
。在响应中是schedule\u id.I。然后用我从rails获得的schedule id分配schedule\u id
这里有点乱,我需要清理一下。但这对我来说很有用:
Client.postSchedule(this.state.date, (schedule) => {
this.setState({schedules: this.state.schedules.concat([schedule])})
}).then(() => Client.postWorker(this.state.worker, this.state.phone, this.state.schedules[this.state.schedules.length - 1].id, (worker) => {
this.setState({workers: this.state.workers.concat([worker])})
})
)
行this.state.schedules[this.state.schedules.length-1].id
有点模糊。它真正做的是,我有一个时间表数组,我在其中存储我的时间表对象。我只是让React知道如何获取数组中的最后一个计划(这将是最新的计划),并为我提供schedules数组中最后一个计划对象的ID
简而言之,(向schedule发送请求)->(等待来自包含我需要的schedule\u id的Rails的响应)->(使用该schedule\u id信息发送另一个请求)
。解决方案是检查,然后检查。在这里,您可以在日程模型中使用rails接受嵌套属性
在日程模型中添加以下行
accepts_nested_attributes_for :workers
然后在SchedulesController中,更新schedule_params方法
def schedule_params
params.require(:schedule).permit(:user_id, :date, workers_attributes: [:id, :name, :phone])
end
在你的react应用程序中
function postSchedule(date, cb) {
return fetch(`api/schedules`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
date: date,
user_id: 1,
workers_attributes: [
{
name: worker_name,
phone: phone
}
]
})
}).then((response) => response.json())
.then(cb); //cb setStates schedules state in main react app
};
现在,只有一个请求可以满足您创建时间表和工作人员的目的
供参考
在这里,您可以在日程模型中使用rails接受嵌套的属性
在日程模型中添加以下行
accepts_nested_attributes_for :workers
然后在SchedulesController中,更新schedule_params方法
def schedule_params
params.require(:schedule).permit(:user_id, :date, workers_attributes: [:id, :name, :phone])
end
在你的react应用程序中
function postSchedule(date, cb) {
return fetch(`api/schedules`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
date: date,
user_id: 1,
workers_attributes: [
{
name: worker_name,
phone: phone
}
]
})
}).then((response) => response.json())
.then(cb); //cb setStates schedules state in main react app
};
现在,只有一个请求可以满足您创建时间表和工作人员的目的
供参考