简体   繁体   English

如何使用 Rails 路由从 React 前端重定向?

[英]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

  1. I am wondering why redirecting from the create action in my Api::V1::ResourcesController doesn't work.我想知道为什么从我的Api::V1::ResourcesController中的create操作重定向不起作用。

  2. 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谢谢

You could try something like this:你可以尝试这样的事情:

redirect_to admin_resources_path, format: :json

Let me know if any luck with that?让我知道是否有运气?

Source 来源

PS Very well formulated question. PS 非常好的问题。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从反应前端应用程序打开新选项卡 - How to open a new tab from a react front-end app 如何从后端通知前端? - How to notify front-end from backend? 在重定向期间将令牌从API传递到前端 - Pass token from API to front-end during redirect Rails-如何将从外部API检索到的前端javascript数据发送到控制器 - Rails - How to send front-end javascript data retrieved from an external API to the controller 如何从Localstorage验证令牌并将currentUser从Apollo Server返回到React前端? - How to validate token from Localstorage and return currentUser from Apollo Server to React front-end? 试图从后端获取配置以响应前端 - trying to get config from backend to react front-end 无法从React前端的ActionCable访问数据 - Cannot access data from ActionCable on React front-end 如何在 React 前端网站上显示来自 Google Drive 的图像? - How do I display images from Google Drive on a react front-end website? 如何从 node/express 中的后端(端口:3001)路由重定向到前端(端口:3000)? - How do I redirect to front-end (port: 3000) from a back-end (port:3001) route in node/express? 如何在React中更改前端的JSON格式 - How to change the format of JSON in the front-end in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM