Rails 4嵌套属性和has_many:通过表单中的associaton
我在管理has_many时遇到问题:通过使用表单进行关联。 我不想做的是编辑相关模型的属性,其中有大量信息,而我只想管理关联。 我理解我可以通过操作传递给我的操作的表单参数并手动构建关系来做到这一点,但我更愿意采用Rails方式,如果可能的话。
关于我的一个有趣的事情是,这个has_many:通过关联实际上是一个我已经使用accepts_nested_attributes_for保存的模型
以下是我的模型:目标,里程碑和程序。
class Goal true end class Milestone :milestone_programs end class Program < ActiveRecord::Base end
现在,在我的目标编辑视图中,我需要能够添加和删除里程碑,对于这些里程碑,我需要能够添加和删除程序关联。 这是我表单的代码。
f.object.id %> f.object.name %> - remove
在我的控制器中,我有
class GoalsController [ :id, :name, :_destroy ]) end end
此表单必须像工作表一样,您可以在最后单击保存时进行更改并保存更改,因此我不相信诸如cocoon或nested_forms之类的gem会有所帮助。
到目前为止,我的代码完美地用于管理我的目标相关里程碑及其属性。 但现在我希望能够管理与这些里程碑相关的程序列表。
我尝试过使用accepts_nested_attributes_for,但这并不是我想要的,因为我不关心编辑模型的嵌套属性,程序属性是保持静态的。
我想我可能会在每个程序的表单中都有这样的东西来构建关联:
但这也不起作用(当然我已经添加了:program_ids到我列入白名单的参数)。 我需要添加到控制器的魔术轨道方法吗?
我在这里想念的是什么?
提前致谢!
使用has_many :through
关系时,需要通过不同的模型传递nested_attributes
,如下所示:
楷模
class Goal < ActiveRecord::Base has_many :milestones, inverse_of: :goal, dependent: :destroy accepts_nested_attributes_for :milestones, :allow_destroy => true def self.build goal = self.new goal.milestones.build.milestone_programs.build_program end end class Milestone < ActiveRecord::Base belongs_to :goal, inverse_of: :milestones has_many :milestone_programs has_many :programs, through: :milestone_programs accepts_nested_attributes_for :milestone_programs end class MilestoneProgram < ActiveRecord::Base belongs_to :milestone belongs_to :program accepts_nested_attributes_for :program end class Program has_many :milestone_programs has_many :milestones, through: :milestone_programs end
调节器
#app/controllers/goals_controller.rb def new @goal = Goal.build end def create @goal = Goal.new(goal_params) @goal.save end private def goal_params params.require(:goal).permit(milestones_attributes: [milestone_programs_attributes: [program_attributes:[]]]) end
形成
#app/views/goals/new.html.erb <%= form_for @goal do |f| %> <%= f.fields_for :milestones do |m| %> <%= m.fields_for :milestone_programs do |mp| %> <%= mp.fields_for :program do |p| %> <%= p.text_field :name %> <% end %> <% end %> <% end %> <%= f.submit %> <% end %>
我很欣赏这可能不是你想要的,但是我没有阅读你的所有散文。 我刚刚收集了你需要帮助通过has_many :through
关系传递nested_attributes