[英]How to redirect from React front-end with Rails routes?
I am new to React and struggling to redirect after making a post request to my api.我是 React 的新手,在向我的 api 发出发布请求后难以重定向。
In an erb view, I am using a javascript pack tag to render some React components on a new resource page.在 erb 视图中,我使用 javascript pack 标签在新资源页面上呈现一些 React 组件。 I am using a button onClick to send my post request (axios) to
api/v1/resources
.我正在使用 onClick 按钮将我的帖子请求(axios)发送到
api/v1/resources
。 I'd like to redirect to admin/resources
after the request is sent.我想在发送请求后重定向到
admin/resources
。
I am wondering why redirecting from the create
action in my Api::V1::ResourcesController
doesn't work.我想知道为什么从我的
Api::V1::ResourcesController
中的create
操作重定向不起作用。
Can I redirect without setting up react router?我可以在不设置反应路由器的情况下重定向吗? If not, how can I set that up?
如果没有,我该如何设置? (All of my routes are defined in Rails).
(我所有的路线都在 Rails 中定义)。
Everything I can find seems to pertain to an entire React front-end when I am only using bits of React in a predominantly Rails front-end.当我只在主要的 Rails 前端中使用一些 React 时,我能找到的一切似乎都与整个React 前端有关。
This is my handleSubmit function that gets called onClick:这是我调用 onClick 的 handleSubmit 函数:
const handleSubmit = () => {
let newResourceParams = {
title: title,
description: description,
occurred_on: occurredOn,
resource_type: resourceType
}
Api.postNewResource(newResourceParams)
}
Here is Api.postNewResource
:这是
Api.postNewResource
:
postNewResource(data) {
return AxiosApi.post('/api/v1/resources', data)
.then(({data}) => data['data'])
.catch((errors) =>
console.log(errors)
)
}
And here is my create
action in Api::V1::ResourcesController
:这是我在
Api::V1::ResourcesController
create
操作:
def create
Resource.create(resource_params)
redirect_to admin_resources_path
end
Any guidance or help on this would be greatly appreciated.对此的任何指导或帮助将不胜感激。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.