Rails 3.2更新嵌套属性

时间:2012-01-24 01:57:29

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

这是我的问题。如果我转到Projects#edit,我将无法更改分配给它的课程。如果我尝试为其创建新课程,或从现有课程中选择,我会收到以下错误:

Couldn't find Course with ID=23 for Project with ID=62
app/controllers/projects_controller.rb:49:in `update'

{...
"project"=>{"title"=>"Sup",
"due_date"=>"2012-01-23",
"course_id"=>"27",                        # THIS IS THE ID OF THE NEW COURSE I WANT
"course_attributes"=>{"name"=>"Calc I",   # THIS IS THE OLD ONE, I don't need this. Why is it passing this?
"number"=>"MATH102",
"user_id"=>"3",
"id"=>"23"},
"description"=>"bla bla bla"},
"commit"=>"Update Project",
"user_id"=>"3",
"id"=>"62"}

所以我可以看到它试图传递course_attributes,但它实际上并没有设置新的course_id。我不明白为什么course_attributes被传递,另一种形式是空白的,而course_attributes被传递,是旧课程的属性。我想将course_id设置为传递的course_id(在这种情况下为27)。

ProjectsController

def new
  @project  = @user.projects.new
  @courses = @user.courses    # Used to populate the collection_select
  @project.build_course       # I was informed I need to use this to get the form_for to work
end

def edit
  @project = Project.find(params[:id])
  @courses = @user.courses    # Used to populate the collection_select
end

def update
  @project = Project.find(params[:id])
  @courses = @user.courses

  if @project.update_attributes(params[:project])
    flash[:notice] = 'Project was successfully updated.'
    redirect_to user_projects_path(@user)
  else
    render :edit
  end
end

第49行是对update_attributes的调用。

其他信息

project.rb

belongs_to :user
belongs_to :course

attr_accessible :course_id, :course_attributes
accepts_nested_attributes_for :course

course.rb

belongs_to :user
has_many :projects

user.rb

has_many :projects
has_many :courses

所以项目在数据库中有一个course_id。我目前正在Projects#new页面上动态创建或选择现有课程。这是我的表格。我使用JavaScript切换来在collection_select和两个text_fields之间交替。

项目/ new.html.haml

= form_for [@user, @project] do |f|
  # This is shown by default
  = f.collection_select :course_id, @courses, :id, :name, { prompt: true }

  .hidden # This is hidden by default and shown using a toggle
    = f.fields_for :course do |builder|
      = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
      = builder.label :number, 'Number'
      = builder.text_field :number, class: 'new_project_course_number'
      = builder.hidden_field :user_id, value: current_user.id

现在,如果我在新项目页面上,并通过选择现有课程将其附加到课程,它将正常工作。将创建项目,并正确设置course_id

如果我在新项目页面上,并使用JavaScript切换创建课程,然后填写课程名称课程编号,然后点击创建那么它也会起作用。将创建课程,并使用正确的course_id创建项目

对于冗长的帖子感到抱歉,但我想提供所有可能的信息。谢谢!

更新1

的routes.rb

resources :users do
  resources :projects do
    collection do
      get 'completed'
      match 'course/:course_number' => 'projects#course', as: 'course'
    end
  end

  resources :courses
end

1 个答案:

答案 0 :(得分:3)

假设您的Projects#edit表单与Projects#new

类似

这是在参数

中创建course_attributes
  .hidden # This is hidden by default and shown using a toggle
    = f.fields_for :course do |builder|
      = builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
      = builder.label :number, 'Number'
      = builder.text_field :number, class: 'new_project_course_number'
      = builder.hidden_field :user_id, value: current_user.id

那是因为如果user有一个当前课程,它将为每门课程创建字段。

如果您希望能够在editnew动态制作新课程,请将此行更改为:

    = f.fields_for :course, @project.build_course(:user => @user) do |builder|

无论您是编辑还是新编辑,都会构建新的course。 (此外,您可以通过这种方式删除控制器中的@project.build_course。)