首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >格式嵌套帮助

格式嵌套帮助
EN

Stack Overflow用户
提问于 2011-07-01 07:06:22
回答 1查看 224关注 0票数 1

我试着让自己熟悉下面的情况。

routine.rb

代码语言:javascript
运行
复制
class Routine < ActiveRecord::Base
  has_many :exercise_routines
  has_many :exercises, :through => :exercise_routines
  accepts_nested_attributes_for :exercise_routines
end

exercise_routine.rb

代码语言:javascript
运行
复制
class ExerciseRoutine < ActiveRecord::Base
  belongs_to :exercise
  belongs_to :routine
  has_many :attempts
  accepts_nested_attributes_for :attempts
end

show.html #/routines/show.html.haml

代码语言:javascript
运行
复制
%h2 Exercises:

%ol
  - for exercise_routine in @routine.exercise_routines
    %li= exercise_routine.exercise.name
    %ul
      %li
        = exercise_routine.sets
        Sets
      %li
        = exercise_routine.reps
        Reps
      %li
        Attempt: 
        - for attempt in exercise_routine.attempts
          = attempt.reps
        = semantic_form_for @routine do |routine|
          = routine.semantic_fields_for exercise_routine do |exercise_routine|
            = exercise_routine.semantic_fields_for :attempts do |attempt|
              = attempt.input :reps, :required => false
          = routine.buttons`

我遇到的问题是两件事: 1)如果我通过控制台手动添加每个例程,这将成功地呈现每个例程的尝试,但如果我尝试更新它,我会得到:“未知属性: exercise_routine”,我意识到这是因为参数:

代码语言:javascript
运行
复制
{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"51fZRe1wKRQoVUz+lGlldd4DmMODGHN203Htc4DEi0Y=",
 "routine"=>{"exercise_routine"=>{"attempts_attributes"=>{"0"=>{"reps"=>"5",
 "id"=>"1"},
 "1"=>{"reps"=>"1555",
 "id"=>"2"},
 "2"=>{"reps"=>""}}}},
 "commit"=>"Update Routine",
"id"=>"3"}`

看起来不太对劲。它应该引用带有id的exercise_routines。

2)希望能够同时添加一次尝试。

代码语言:javascript
运行
复制
@routine.exercise_routines.each do |er|
  1.times {er.attempts.build}
end

正确地呈现了它,但当然我也遇到了同样的参数问题。我要么做了一些愚蠢的错误,要么这比我想象的更难做这种类型的多集合嵌套?

EN

回答 1

Stack Overflow用户

发布于 2011-07-01 09:05:11

您没有正确使用fields_for。第一个参数必须是用于嵌套字段的关联。然后,第二个可以是用于关联值的/单个模型的集合。

代码语言:javascript
运行
复制
= routine.semantic_fields_for :exercise_routines, exercise_routine do |f|

另外,我建议不要将第一个带有表单构建器的本地exercise_routine隐藏在块中。并且我将使用@routine.exercise_routines.each do |exercise_routine|表单而不是for循环。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6542062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档