2013-12-13 81 views
0

我正在爲一個有嵌套模型的非常大型的類的項目工作。下面是對形式重要的車型,以及他們的協會:如何在不使用accept_nested_attributes_for的情況下更新Rails中的嵌套屬性?

  • 課程:的has_many:time_blocks,的has_many:標籤,通過:引用的Tagging,belongs_to的:機構的has_many:角色的has_many:用戶通過:角色

  • TimeBlock:belongs_to的:當然

  • 標籤:的has_many:
  • 的Tagging
  • 標記:belongs_to的是:標籤,belongs_to的:taggable_type
  • 機構:的has_many:課程,的has_many:用戶
  • 角色:belongs_to的:當然,belongs_to的:用戶

我能夠正確地創建嵌套形式,但我不能讓嵌套模式,以正確更新。這裏是控制器,表單非常長,但是我爲嵌套模型提供了參數。請注意,我清除了params中的值,但一些參數具有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"=>[]} 

標籤PARAMS

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

爲什麼你不希望使用'accepts_nested_attributes_for'列出的鍵名? – sevenseacat

回答

0

如果你不能使它與accepts_nested_attributes_for工作,那麼你就必須寫自己的setter方法(S)手動。喜歡的東西:

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

方法名(在我的例子tag_attributes =)需要相匹配的標籤PARAMS正在

相關問題