如何在没有accepts_nested_attributes_for的情况下更新Rails中的嵌套属性?

时间:2013-12-13 01:32:30

标签: ruby-on-rails nested-forms nested-attributes

我正在为一个类的项目工作,我有一个非常大的形式与嵌套模型。以下是对表单重要的模型及其关联:

  • 课程:has_many:time_blocks,has_many:tags,through :: taggings,belongs_to:institution,has_many:roles,has_many:users,through :: role

  • TimeBlock:belongs_to:course

  • 标签:has_many:taggings
  • 标记:belongs_to:tag,belongs_to:taggable_type
  • 机构:has_many:courses,has_many:users
  • 角色:belongs_to:course,belongs_to:user

我能够正确创建嵌套表单,但我无法让嵌套模型正确更新。这是控制器,表单很长,但我已经为嵌套模型提供了参数。注意,我清除了参数中的值,但是一些参数具有ID值,因为它们存在于db中。我还包括CoursesHelper来显示我在控制器中使用的辅助方法。

应用/控制器/ courses_controller.rb

  def new
    @course = current_user.courses.new

    @course.institution = Institution.new

    4.times { @course.tags.build }
    7.times { @course.time_blocks.build }
  end

  def create
    @course = Course.new(params[:course])

    @course.institution = Institution.new(params[:institution])

    filled_tags = set_tags(params[:tag])
    @course.tags.build(filled_tags)

    filled_time_blocks = set_time_blocks(params[:time_block])
    @course.time_blocks.build(filled_time_blocks)

    if @course.save
      Role.create!(
        user_id: current_user.id, 
        course_id: @course.id, 
        title: 'instructor'
      )

      redirect_to @course
    else
      (4 - filled_tags.count).times { @course.tags.build }
      (7 - filled_time_blocks.count).times { @course.time_blocks.build }

      flash.now[:errors] = @course.errors.full_messages
      render :new
    end
  end

  def edit
  end

  def update
    filled_time_blocks = set_time_blocks(params[:time_block])
    filled_time_blocks.each do |time_block| 
      @course.time_blocks.update_attributes(time_block)
    end 

    filled_tags = set_tags(params[:tag])
    filled_tags.each { |tag| @course.tags.update_attributes(tag) }
    # @course.tags.update_attributes(filled_tags)

    # @course.time_blocks.update_attributes(filled_time_blocks)
    fail
    if @course.update_attributes(params[:course])
      redirect_to @course
    else
      flash.now[:errors] = @course.errors.full_messages
      render :edit
    end
  end

应用/助手/ courses_helper.rb

  def set_time_blocks(entries)
    result = []

    days = entries[:day_of_week].reject! { |day| day.blank? }

    days.each do |day|
      time_block = {}

      time_block[:day_of_week] = day
      time_block[:start_time] = entries[day][:start_time]
      time_block[:end_time] = entries[day][:end_time]
      time_block[:id] = entries[day][:id]

      result << time_block
    end

    result
  end

  def set_tags(entries)
    [].tap do |tags|
      entries.each do |entry|
        tags << entry unless entry.values.all?(&:blank?)
      end
    end
  end

  def find_course
    if params.include?(:id)
      @course = Course.find(params[:id])
    else
      flash[:notice] = "Sorry, Could Not Find Course."
      redirect_to current_user
    end
  end

TimeBlock Params

{"sun"=>{"start_time"=>"", "end_time"=>"", "id"=>""}, "mon"=>{"start_time"=>"", "end_time"=>"", "id"=>"3"}, "tue"=>{"start_time"=>"", "end_time"=>"", "id"=>"4"}, "wed"=>{"start_time"=>"", "end_time"=>"", "id"=>"5"}, "thu"=>{"start_time"=>"", "end_time"=>"", "id"=>"6"}, "fri"=>{"start_time"=>"", "end_time"=>"", "id"=>"7"}, "sat"=>{"start_time"=>"", "end_time"=>"", "id"=>""}, "day_of_week"=>[]}

标记参数

[{"name"=>"", "id"=>"4"}, {"name"=>"", "id"=>""}, {"name"=>"", "id"=>""}, {"name"=>"", "id"=>""}]

1 个答案:

答案 0 :(得分:0)

如果你无法使用accepts_nested_attributes_for,那么你必须手动编写自己的setter方法。类似的东西:

class Course < ActiveRecord::Base
  def tag_attributes=(tags)
    tags.each do |tag|
      self.tags.build(tag)
    end
  end
end

方法名称(在我的示例中为tag_attributes =)需要匹配

下列出的标记参数的键名称