更新与现有数据的嵌套形式的内在形式使用Rails 3 [英] Updating nested forms with existing data in inner form using rails 3

查看:153
本文介绍了更新与现有数据的嵌套形式的内在形式使用Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让嵌套表格视图正确更新。但是,这是造成问题时,第二种形式有存在的数据。

I'm trying to get a nested form view to update properly. This is however causing problems when the second form has existing data.

我使用accepts_nested_attributes_for和nested_form_for。第二个它唯一的目的就是动态地添加使用JS表单元素。请参见 github上更多

I'm using accepts_nested_attributes_for and nested_form_for. The second which only purpose is to dynamically add the form element using js. See github for more

我得到的错误是:

Couldn't find Muscle with ID=3685340 for Exercise with ID=212831413

我试图做手工更新,但我的code真正地没有工作,我的IM pression,它不应该是必要的,因为轨道是假设照顾它的引擎盖下下我。

I've tried to manually do the updating but my code didnt really work and I'm under the impression that it shouldnt be needed since rails is suppose to take care of it under the hood.

的想法是,:锻炼具有穿过目标许多肌肉
而且从锻炼形式中我希望能够增加目标肌肉。

The idea is that: Exercises has many Muscles through Targets And from within the Exercise form I want to be able to add target muscles.

我的模型:

class Exercise < ActiveRecord::Base
  has_many :targets, :dependent => :destroy
  has_many :muscles, :through => :targets
  accepts_nested_attributes_for :muscles, :reject_if => :all_blank
  ...
end

class Target < ActiveRecord::Base
  belongs_to :exercise
  accepts_nested_attributes_for :exercise, :update_only => true
  belongs_to :muscle
end

class Muscle < ActiveRecord::Base
  has_many :targets, :dependent => :destroy
  has_many :exercises, :through => :targets
end

我的(HAML)查看:

My (haml) view:

%p
  %b Target(s):
  = f.fields_for :muscles do |e|
    = e.collection_select :id, Muscle.all, :id, :name
    = e.link_to_remove "-remove"
  = f.link_to_add "Add target muscle", :muscles

最后我出现故障的控制器:

And finally my failing controller:

  def update
    @exercise = Exercise.find(params[:id])
    @exercise.user = current_user

    params[:exercise][:muscles_attributes].each { |id, muscle|
    target = Target.where(:exercise_id => @exercise.id , :muscle_id => muscle[:id]).first
    if target && !(muscle[:_destroy] == "false")
      puts "!!>>>>>>>>>>>>>>>>>>destroy target #{target.exercise_id} #{target.muscle_id}"
      target.destroy
    else
      if !target
        t = @exercise.targets.build(:muscle_id => muscle[:id])
        t.save
      end
    end
  }

  respond_to do |format|
    if @exercise.update_attributes(params[:exercise])
      format.html { redirect_to(@exercise, :notice => 'Exercise was successfully updated.') }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @exercise.errors, :status => :unprocessable_entity }
    end
  end
end

请让我知道我是否应该公开更多的我的code的(最终的结果将是开源反正)生病愉快地推到github上或者被请求什么,先谢谢了。

Please let me know if I should expose more of my code (the final result will be opensource anyway) ill happily push to github or whatever is requested, thanks in advance.

推荐答案

您可以通过操纵中间表,这里目标

You could achieve this by manipulating the intermediate table, here targets.

插入模型:

accepts_nested_attributes_for :targets

和在HTML,添加此行的javascript:

And in your html, append this line with javascript:

<input name="exercise[targets_attributes][0][muscle_id]" value="the_muscle_id_goes_here" type="hidden">

这篇关于更新与现有数据的嵌套形式的内在形式使用Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆