簡體   English   中英

在沒有accepts_nested_attributes_for的情況下,如何在Rails中更新嵌套屬性?

[英]How do I update Nested Attributes in Rails without accepts_nested_attributes_for?

我正在為一個班級的項目工作,其中的嵌套模型具有很大的形式。 以下是對表單及其關聯很重要的模型:

  • 課程:has_many:time_blocks,has_many:tags,通過::taggings,belongs_to:institution,has_many:roles,has_many:users,通過::roles

  • TimeBlock:belongs_to:課程

  • 標簽:has_many:taggings
  • 標記:belongs_to:tag,belongs_to:taggable_type
  • 機構:has_many:courses,has_many:users
  • 角色:belongs_to:課程,belongs_to:用戶

我能夠正確創建嵌套表單,但是無法正確更新嵌套模型。 這是控制器,形式很長,但是我提供了嵌套模型的參數。 注意,我從參數中清除了值,但是某些參數具有ID值,因為它們存在於db中。 我還包括了CoursesHelper,以顯示我在控制器中使用的幫助器方法。

app / controllers / 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

app / helpers / 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

時間塊參數

{"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"=>""}]

如果您無法使其與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 =)需要與標簽參數在下面列出的鍵名匹配

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM