ruby-on-rails - rails_to 用于重复记录
问题描述
以下在控制台中可以正常工作并且可以执行我想要的操作(第一行会这样做,其他行只是为了模拟更改。实际上它是真实的,这些是唯一需要的更改):
irb(main):012:0> year = Year.find(678).dup
Year Load (0.4ms) SELECT "years".* FROM "years" WHERE "years"."id" = $1 ORDER BY "years"."year_date" ASC LIMIT $2 [["id", 678], ["LIMIT", 1]]
=> #<Year id: nil, year_date: "1905-09-01", created_at: nil, updated_at: nil, resto: false, resid: true, file_basename: nil, person_id: 86, location_id: 95, title: "Resident", notes: "", resto_name: "", aws_url: nil, from_where_url: nil, caption: nil, croatian: true, from_where: nil, doc_id: 66>
irb(main):013:0> year.location_id = 211
=> 211
irb(main):014:0> year.resto = true
=> true
irb(main):015:0> year.resid = false
=> false
irb(main):016:0> year.title = "Co-proprietor"
=> "Co-proprietor"
irb(main):017:0> year.save
(0.3ms) BEGIN
Location Load (0.5ms) SELECT "locations".* FROM "locations" WHERE "locations"."id" = $1 LIMIT $2 [["id", 211], ["LIMIT", 1]]
Person Load (0.3ms) SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT $2 [["id", 86], ["LIMIT", 1]]
Doc Load (0.2ms) SELECT "docs".* FROM "docs" WHERE "docs"."id" = $1 LIMIT $2 [["id", 66], ["LIMIT", 1]]
Year Create (5.3ms) INSERT INTO "years" ("year_date", "created_at", "updated_at", "resto", "resid", "person_id", "location_id", "title", "notes", "resto_name", "croatian", "doc_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["year_date", "1905-09-01"], ["created_at", "2020-05-10 15:12:37.102224"], ["updated_at", "2020-05-10 15:12:37.102224"], ["resto", true], ["resid", false], ["person_id", 86], ["location_id", 211], ["title", "Co-proprietor"], ["notes", ""], ["resto_name", ""], ["croatian", true], ["doc_id", 66]]
(40.9ms) COMMIT
=> true
当然,我会在一个edit
或new
页面中进行更改。我只想用show
我所在页面的信息副本创建该页面。用例正在创建一堆记录,其中许多几乎是重复的,但确实需要手动完成,因为更改的信息没有模式。
我创建了各种按钮,但没有任何效果
<%= link_to 'Duplicate this connection (FIXME)', new_year_path(@year.dup), action: dup %>
和:
def dup
@year = Year.find(params[:id]).dup
end
另一个迭代:<%= link_to 'Duplicate this connection (FIXME)', edit_year_path(@year.dup) %>
No route matches {:action=>"edit", :controller=>"years", :id=>nil}, missing required keys: [:id]
我对此感到迷茫,但可能并不难。
解决方案
当您#.dup
在此处调用 ActiveRecord 模型对象时,它会复制除id
基本时间戳之外的所有属性。这意味着您有一个未持久的对象。这就是您收到异常消息的原因。
假设您要复制记录 678,比方说,我希望这样的路径:
/years/new?base_id=678
在上面,base_id=678
是一个查询字符串参数。
你会像这样生成它:
<%= link_to "Duplicate", new_year_path(base_id: @year&.id) %>
(假设@year
是初始化,当然)
然后,在您的控制器操作中:
def new
@year = Year.find_by(id: params[:base_id])&.dup || Year.new
end
假设我们找到有Year
问题的记录,我们复制它。否则,我们会退回到一个新的Year
对象,生活就很好。
这应该可以解决您的问题。
推荐阅读
- c++ - 使用ASIO为ffmpeg实现interrupt_callback
- oracle - Oracle 顶点未打开
- python - 将字典与列表中的“重复”键结合起来
- javascript - 删除目录 - removeRecursively 过时
- node.js - 使用 serve 命令在生产中启动节点/表达一个反应应用服务器
- ag-grid - 如何对出现在 ag-Grid 和 AdapTable 的过滤器列表中的列值进行排序?
- python - Pandas groupby 多列然后子图
- c - 试图尝试cs50中的可读性问题。无法计算字符串中的字母
- javascript - 创建一个函数 funcA,它接收一个数字数组并在 2 秒后返回它们的总和
- pine-script - 我不能每隔几个柱计算一个系列值,它是一个错误吗?