【发布时间】:2015-05-23 22:36:09
【问题描述】:
我有一个处理调查及其答案的嵌套表单。但是我在加载表单时遇到了一个奇怪的错误:
ActiveRecord::HasManyThroughNestedAssociationsAreReadonly
有什么想法吗?我不确定我应该如何修复这些关联。
<%= form_for @survey do |f| %>
...
<%= f.fields_for :answers do |builder| %>
<%= builder.text_field :content, :class=>"form-control" %>
<% end %>
...
<% end %>
调查#新
def new
@survey = Survey.new
@template = Template.find(params[:template_id])
@patient = Patient.find(params[:patient_id])
@survey.answers.build
end
调查.rb
class Survey < ActiveRecord::Base
belongs_to :template
has_many :questions, :through=> :template
has_many :answers, :through=> :questions
accepts_nested_attributes_for :answers
end
模板.rb
class Template < ActiveRecord::Base
belongs_to :survey
has_many :questions
end
问题.rb
class Question < ActiveRecord::Base
belongs_to :template
has_many :answers
end
答案.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
【问题讨论】:
-
嗨!我遇到了类似的问题,你有没有找到一个可行的解决方案?
标签: ruby-on-rails forms nested