在 Ruby on Rails 中渲染参数或重定向错误
我正在 Rails 上使用 ruby,并且我的网站上有一些内容可供用户单击保存,并且它们会被重定向到具有登录和注册的页面,以便用户可以继续使用任一选项并保存内容。这会产生显示正确的用户验证错误的问题,b/c我需要使用redirect_to users/new来传递带有用户正在保存的对象id的参数,如果我使用render :new,错误是显示但对象 ID 丢失。有人对此有任何解决方案吗?
I am using ruby on rails and have things on my site that users can click to save and they are redirected to a page with both a login and a signup so that the user can continue with either option and save the content. The creates a problem for showing the proper user validation errors, b/c I need to use a redirect_to users/new in order to pass the params with the object id that the user is saving and if I use render :new, the errors are displayed but the object id is lost. Anyone have any solutions for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将原始项目 ID 存储在会话中,继续正常的登录/注册过程,完成后,如果会话中有保存项目,则重定向到处理保存的操作(现在可以从会话并继续)。
Store the original item id in the session, proceed with your normal login/signup process, when that completes, if there is a save item in the session, redirect to the action that handles the save (it can now grab the item id from the session and proceed).
“出于好奇,将对象本身保存在会话中有什么问题吗?这样我就不必执行第二次数据库查找来再次找到该对象。” ——十杰克
(这可能是一个新的 StackOverflow 问题)
在会话中保存项目是一件坏事 - 因为当您迁移模型对象时(例如添加列或类似的东西),会话中的数据现在不再是模型类型的有效对象。例如,它仍然具有旧的属性列表而不是新的......并且它将显示为无效对象。
这就是为什么最好只存储 id - 因为您将从数据库中获取一个新的、正确实例化的对象。
"Out of curiosity, what is wrong with saving the object itself in the session? That way I wouldn't have to perform a second database lookup to find the object again." --TenJack
(this should probably be a new StackOverflow question)
Saving an item in the session is a Bad Thing - because the moment you migrate your model object (eg to add a column or something similar), the data in the session is now no longer a valid object of the model's type. eg it will still have the old attribute-list instead of the new one... and it will appear as an invalid object.
This is why it's best to store only the id - because you will fetch a fresh, correctly instantiated object from the db.