Rails Controller 中带有 URL 参数的渲染操作
在我的场景中,我在 URL
localhost:3000/products/new?product_type='abc'
上有一个表单,当我填写表单并点击“保存”时,由于验证错误,它将运行 Rails 块:如下:
format.html {
render :new,
status: :unprocessable_entity
}
它将浏览器渲染到 localhost:3000/products/new
但就我而言,我想渲染到我开始的同一页面以及用户填写的 Product表格
..
什么可能是最好的解决方案,以便用户应该保留在上一页以及 url 中的数据和参数。
作为
localhost:3000/products/new?product_type='abc'
提前致谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在任何形式提交后要留在页面上,这很简单,您必须使用
在这里您可以看到我通过
远程:true
作为其他参数,将您的表单作为ajax请求发送为ajax请求,而不是刷新当前表单页面。返回JSON作为错误响应:
您还可以使用 page.js.erb 扩展程序创建一个页面,以处理您的JavaScript代码。
That's very simple If you want to stay in page after any form submission you have to use Rails "Ajax helpers"
Here you can see I pass
remote: true
as additional params that will send your form as Ajax request instead for refresh current form page.Return Json as error response:
You can also create a page with page.js.erb extension to handle your javascript code.