在手动反向重定向时保留表单数据的更好方法?
我有一个表单,在提交时要求客户查看数据,然后在保存数据之前确认更改。
但是,如果用户在提交之前点击后退按钮返回并查看数据,我会使用一种非常粗略的方法来保留数据。由于我希望用户对字段而不是当前数据库字段的更新成为此“返回”操作中显示的内容,因此我必须通过 post/get 参数传递值。所以我的控制器看起来像:
@customer = Customer.find session[:id]
unless params[:customer].nil?
@customer.attributes = params[:customer]
end
有更好的方法吗?我实际上很好奇普遍接受的 Rails 会话用法是什么,因为我很确定使用会话来传递这样的信息是禁忌。是的,不是?想法?谢谢。
I have a form that, on submit, requires the customer to look over the data, and then confirm the changes before saving the data.
However, I'm using a really rough way of retaining data if the user hits the back button to go back and look over the data before submitting. Since I want the user's updates to the fields and not the current database fields, to be what displays on this "go back" action, I have to pass the values around via post/get params. So my controller looks something like:
@customer = Customer.find session[:id]
unless params[:customer].nil?
@customer.attributes = params[:customer]
end
Is there a better way to do this? I'm actually really curious what the generally accepted Rails session usage is as I'm pretty sure using the session for passing info like this is taboo. Yes, no? Thoughts? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有更好的方法来做到这一点,涉及会话。我宁愿完全避免使用会话哈希,而是使用第二种形式和大量 JavaScript。
这是我的处理方式。
第一个表单根本不会发生太大变化,只需提交一个名为“审核”的新操作即可。
审核操作将显示更改以供审核,并具有可见的编辑链接和确认按钮。它还将在隐藏元素中重新创建上一页的表单,该元素将再次提交给审核操作。
确认按钮将提交您的更新操作。
单击编辑链接将隐藏审阅区域并显示与上一页相同的内容。
下面是一些示例代码来强调这一点:
app/controllers/example_controller.rb
app/views/examples/_form.html.erb:
app/views/示例/edit.html.erb:
app/views/examples/review.html.erb:
There may be a better way to do it involving sessions. I would rather avoid using the session hash entirely, with a second form and a lot of javascript.
Here's how I'd handle it.
The first form wouldn't change much at all, just submit to a new action called review.
The review action will display the changes for review, and have an edit link and a confirm button visible. It will also recreate the form from the previous page in a hidden element which will submit again to the review action.
The confirm button would submit to your update action.
Clicking the edit link will hide the review area and show duplicated from from the previous page.
Here's some sample code to hammer down the point:
app/controllers/example_controller.rb
app/views/examples/_form.html.erb:
app/views/examples/edit.html.erb:
app/views/examples/review.html.erb: