多个视图上的相同部分返回到调用它的任何视图
我目前正在两个不同视图上重复使用部分内容。
查看A
查看B
部分属于模型 A,但与模型 B 具有关联,因此它也适用于模型 B。
它包含一个表单,当提交数据时,它总是将用户重定向到视图 A。但是,当我从视图 B 提交表单时,我希望重定向回视图 B 而不是表单 A。
它重定向的原因是正确的现在转到视图 A 是因为这是该表单所属的模型。 因此,当发布时,它与控制器 A 对话并使用重定向将用户带到 a_url。
我怎样才能告诉我的表单(或者控制器操作)将用户重定向回他们来自的地方?
谢谢!
I'm currently re-using a partial on two different views.
View A
View B
The partial belongs to Model A but has an association with Model B so it is applicable to Model B as well.
It contains a form and when data is submitted, it always redirects the user to View A. However, when I submit the form from View B, I would like to be redirected back to View B instead of Form A.
The reason it redirects right now to View A is because that's the model this form belongs to. So when posted, it talks to controller A and uses a redirect take the user to a_url.
How can I tell my form (or more so that controller action) to redirect the user back to where they came from?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了。
我在表单中添加了一个隐藏字段,其中包含渲染部分的控制器名称,然后我的 respond_to 块确定将用户发送到哪里。
查看代码:
我的控制器代码:
Solved.
I added a hidden field to my form that contained the controller name of where the partial was rendered and then my respond_to block determined where to send the user.
View code:
My controller code:
,而不是使用隐藏标记
您可能希望将其放置在会话中和其他操作中
。切换表单变量非常容易,而伪造会话可能更困难(但并非不可能)。 如果这是我的申请,我会走这条路。
Instead of using a hidden tag, you may want to place this in the session:
and in the other action..
It's pretty easy to switch out form variables, whereas it may be harder (but not impossible) to forge a session. I'd go this route if it were my application.