Ruby on Rails:ActiveRecord 对象创建的确认页面 [英] Ruby on Rails: Confirmation Page for ActiveRecord Object Creation
问题描述
使用 Ruby on Rails 在创建 ActiveRecord 对象之前,我需要一个确认页面.用户将在提交之前看到他们正在创建的项目的预览以及正在保存在数据库中的对象
Using Ruby on Rails I want a confirmation page before creating an ActiveRecord object. The user will see a preview of the item they are creating before submitting and the object being saved in the database
一种常见的模式;
- 用户访问/entry/new
- 用户输入详细信息并点击提交
- 用户被重定向到/entry/confirm,其中显示条目并点击提交或编辑以更正错误
- 对象已保存
您将如何实施它?
推荐答案
我可能会向该模型的 routes.rb 文件添加预览"操作:
I would probably add a "preview" action to the routes.rb file for that model:
map.resource :objects, :new => { :preview => :post }
您可以通过发布 preview_object_url
命名路由来执行此 preview
操作.您基本上需要以与在 create
操作中相同的方式创建 Object
,如下所示:
You would get to this preview
action by POSTing the preview_object_url
named route. You would need to essentially create the Object
in the same way you would in your create
action, like this:
def preview
@object = Object.new(params[:object])
end
此页面然后将 POST 到 create
操作,然后该操作将创建对象.很简单.
This page would then POST to the create
action, which would then create the Object. It's pretty straight forward.
http://api.rubyonrails.org/classes/ActionController/Resources.html
这篇关于Ruby on Rails:ActiveRecord 对象创建的确认页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!