leo*_*r07 10 ruby forms ruby-on-rails
我是Rails的新手,我正在做我的第一个项目.另外,英语不是我的母语,请耐心等待.
我遇到的问题是我有一个具有相同模型的多个实例的表单,正在正确创建数据但是当我尝试编辑它时,表单以错误的方式填充.
我正在制作一个应用来检查一切是否符合规则.要检查的项目位于嵌套关联章节 - >子章节 - >检查中
每次提交检查时,都会创建一个CheckRound,并且每个检查的信息都会单独存储在CheckResults中.
CheckRounds
has_many :check_results, inverse_of: :check_round, dependent: :destroy
accepts_nested_attributes_for :check_results, reject_if: proc { |att| att['observation'].blank? }
Run Code Online (Sandbox Code Playgroud)
CheckResults
belongs_to :check_round, optional: true, inverse_of: :check_results
belongs_to :check
Run Code Online (Sandbox Code Playgroud)
章
has_many :subchapters
Run Code Online (Sandbox Code Playgroud)
子章节
belongs_to: chapter
has_many: checks
Run Code Online (Sandbox Code Playgroud)
检查
belongs_to :subchapter
has_many :check_results
Run Code Online (Sandbox Code Playgroud)
该表单显示所有章节和嵌套的子章节和检查.每个Check都显示其名称,并将text_area作为输入.
用户可以填写任何一个或多个支票.
<%= form_for(@check_round, :url => {:action => 'update', :client_id => @client.id, :project_id => @project.id}) do |f| %>
<% @chapters.each do |chapter| %>
<%= chapter.name %>
<% chapter.subchapters.each do |subchapter| %>
<%= subchapter.name %>
<% subchapter.checks.each do |check| %>
<%= f.fields_for :check_results do |result| %>
<%= check.name %>
<%= result.hidden_field(:check_id, :value => check.id) %>
<%= result.text_area(:observation, rows: 4, :id =>'obs' + check.id.to_s) %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
控制器是
def edit
@check_round = CheckRound.includes(:check_results).find(params[:id])
@chapters = Chapter.includes(subchapters: :checks).where("segment_id = ?", @project.segment_id).sorted
end
Run Code Online (Sandbox Code Playgroud)
例如,如果,我认为check.id = 3
有observation = "bad"
,当我走在它的观测编辑每张支票都有"坏"无论其ID的.
我想知道如何编辑所有带有空白观察的检查但是创建的检查.
在此先感谢您的时间!
这是我承诺的解决方案。
Sinds 你已经定义了带有空白观察的检查结果必须被拒绝,并且你的 erb 本身会涉及很多逻辑,我会把它全部放在一个辅助方法中,这样你的 erb 就会更干净。像这样的东西:
#helpers/check_rounds_helper.rb
def edit_or_instantiate_nested_check_results(f, check_round, check, new_check_result)
if check.check_results
f.fields_for :check_results, check_round.check_results.where(check_id: check.id) do |result|
result.hidden_field(:check_id, :value => check.id)
result.text_area(:observation, rows: 4, :id =>'obs' + check.id.to_s)
end #end for the already present check results
# if u want to add a new check result event if the check is populated
f.fields_for :check_results, new_check_result do |new|
new.hidden_field(:check_id, :value => check.id)
new.text_area(:observation, rows: 4, :id =>'obs' + check.id.to_s)
end #end for the new check result
else #if there is no existing check result nest a form for a new one
f.fields_for :check_results, new_check_result do |new|
new.hidden_field(:check_id, :value => check.id)
new.text_area(:observation, rows: 4, :id =>'obs' + check.id.to_s)
end #end for the new check result
end #end if statement
end
Run Code Online (Sandbox Code Playgroud)
那么在你看来:
<%= form_for(@check_round, :url => {:action => 'update', :client_id => @client.id, :project_id => @project.id}) do |f| %>
<% @chapters.each do |chapter| %>
<%= chapter.name %>
<% chapter.subchapters.each do |subchapter| %>
<%= subchapter.name %>
<% subchapter.checks.each do |check| %>
<%= check.name %>
<% new_check_result = CheckResult.new(check_round_id: @check_round.id, check_id = check.id) %>
<%= edit_or_instantiate_nested_check_results(f, @check_round, check, new_check_result) %>
<% end %>
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
应该就是这样;)。让我知道它是否有效:D!
克罗恩,