Ruby on Rails的:确认页面的ActiveRecord对象创建对象、页面、on、Ruby

2023-09-08 17:28:35 作者:мé、引领潮流

使用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

一个常见的​​模式;

A common pattern;

在用户访问/项/新 在用户输入的信息并点击提交 在用户被重定向到/项/确认它显示的条目,点击提交或编辑,纠正错误 在对象被保存

你将如何实现它?

推荐答案

我可能会增加一个preVIEW行动的routes.rb中文件的模型:

I would probably add a "preview" action to the routes.rb file for that model:

map.resource :objects, :new => { :preview => :post }

您将获得本 preVIEW 张贴的 preview_object_url 命名路由操作。您将需要以同样的方式你会在你的创建操作,这样从根本上创建对象

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

该网页将然后发布到创建动作,然后将创建对象。这是pretty的直线前进。

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