只需尝试渲染显示页面

时间:2017-07-17 20:47:46

标签: ruby-on-rails ruby-on-rails-3.2 activeadmin

这是show.html.arb文件:

attributes_table_for resource do
  row :id
  row :state
  row :request_type
  row :assignee
  row :assigner
  row :unassigned
  row :deleter
end

attributes_table_for resource.request do
  row :id
end

这是ActiveAdmin.register请求部分:

show do |resource|
  render 'show'
end

以下是用于呈现此页面的链接:

<%= content_tag(:li, link_to('View', accounting_request_path(accnt_request))) %>

我的问题是,为什么它告诉我它试图删除关联?

这是GET请求,而不是PUT或POST请求。

这是确切的错误:

Failed to remove the existing associated request. The record failed to save when after its foreign key was set to nil.

确实,当我只是查看此记录的显示页面时,删除关联。 The @2nd column is the association's id

1 个答案:

答案 0 :(得分:0)

这个问题没有显示出来 - 但其中一个模型是在其中声明的after_initialize回调。

日志告诉我这个方法在这里被称为build_requestable

app/models/concerns/requestable.rb:6:in `build_requestable'
app/views/accounting_requests/_show.html.arb:6:in `block in _app_views_accounting_requests__show_html_arb___512970926778810589_70108054037800'
app/views/accounting_requests/_show.html.arb:1:in `new'
app/views/accounting_requests/_show.html.arb:1:in `_app_views_accounting_requests__show_html_arb___512970926778810589_70108054037800'
app/ui/stewgle/tasks/accounting_requests.rb:33:in `block (2 levels) in <top (required)>'
lib/monkies/aa_views_pages_base.rb:62:in `block in build_page_content'
lib/monkies/aa_views_pages_base.rb:60:in `build_page_content'
app/controllers/application_controller.rb:57:in `set_time_zone'

现在,这是文档中关于此回调的内容:

  

最后触发after_find和after_initialize回调   每个对象由查找程序找到并实例化   在将新对象实例化为之后触发after_initialize   好。

这意味着只需执行ar_object_thingy.associated_thingy即可调用after_initialize回调。在该回调中,我正在做self.build_thingy(示例)以在多态关系中创建对象。这实际上正在破坏与导致错误的非持久对象的当前关系。

TLDR;

在这种情况下,问题不在于ActiveRecord。这是一个我正在使用的模型回调调用after initialize,它调用一个方法来打破belongs_to关系,这个模型验证每次我只是通过父级查找父记录的子项时的存在。