带有嵌套属性的Rails表单来自连接模型many_to_many

时间:2014-04-29 23:06:02

标签: ruby-on-rails many-to-many nested-attributes

我有两个问题:

- 我坚持创建一个包含嵌套属性的项目:position

- 我几乎用于编辑项目详细信息和:position属性,但fields_for:assigned_projects广告分配给项目的所有用户的所有字段。

我有3个型号:

class User < ActiveRecord::Base
  has_many :assigned_projects
  has_many :projects, :through => :assigned_projects

  has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :assigned_projects
  has_many :users, :through => :assigned_projects
  belongs_to :creator, :class_name => "User", :foreign_key => :creator_id

  attr_accessible :name, :controlled, :currency, :creator_id, :assigned_projects  
  accepts_nested_attributes_for :assigned_projects#, :allow_destroy => true
end

class AssignedProject < ActiveRecord::Base
  belongs_to :user,    class_name: "User"
  belongs_to :project, class_name: "Project"

  attr_accessible :project_id, :user_id, :position, :project, :user, :user_attributes
  accepts_nested_attributes_for :user
end

每个用户都可以创建一个Project并且是Projects.creator 每个项目都有__用户通过加入模型Assigned_Project

每个用户在项目中可以有不同的位置,所以我想在连接模型AssignedProject中保存:位置。

如果用户创建了一个Project,他应该能够编辑项目属性PLUS:新连接模型的position属性。

现在是New.Project和Edit.Project的表单字段

/project/new.htm.erb

<%= form_for( setup_new_project(@project) ) do |f| %>

    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.fields_for :assigned_projects do |ff| %>
        <%= ff.label :position %>
        <%= ff.text_field :position%>
    <% end %>

    <%= f.submit "Add Project", class: "" %>                    
<% end %>


/project/edit.htm.erb

<%= form_for( setup_project(current_project) ) do |f| %>

    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.fields_for :assigned_projects do |ff| %>
        <%= ff.label :position %>
        <%= ff.text_field :position%>
    <% end %>

    <%= f.submit "Update Project", class: "" %>
<% end %>

我有以下本文所述的设置方法: http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/

module FormHelper
    def setup_project(project)
        project.assigned_projects ||= AssignedProject.new
        project
    end

    def setup_new_project(project)
        project.assigned_project = AssignedProject.new
        project
    end

end

我希望问题很清楚。

用于创建新项目的当前错误消息为:

undefined method `assigned_project='

15:             <%= render 'shared/user_sidebar_menu' %>
16: 
17:             <div class="span4 offset1">
18:                 <%= form_for( setup_new_project(@project) ) do |f| %>
19: 
20:                     <%= render 'shared/error_messages', object: f.object %>
21: 

更新:添加了projects_controller.rb

projects_controller.rb

class ProjectsController < ApplicationController

    def new
        @project = Project.new
    end

    def create
        @project = Project.new(params[:project])
        @project.creator = current_user
        if @project.save
            current_user.assigned_projects.create(project: @project)
            redirect_to current_user
        else
            render 'new'
        end
    end
end

1 个答案:

答案 0 :(得分:0)

更新setup_new_project方法如下:

   def setup_new_project(project)
        project.assigned_projects.build ## Updated this line
        project
    end

使用project.assigned_projects(注意复数)代替project.assigned_project(注意单数错误)。

UserAssignedProject模型属于1-M关系。因此,您获得了动态方法assigned_projects=(注意复数),当您调用不存在的assigned_project=(注意单数)时,您会收到错误

<强>更新

undefined method each for <AssignedProject:0x007ff7aa55b528>

使用project.assigned_projects.build代替project.assigned_project = AssignedProject.new

更新2

你正在接近这个错误。表格助手完全不是必需的。您需要做的就是更新newcreate操作,如下所示:

def new
    @project = Project.new
    @project.assigned_projects.build
end

并更新form_fornew视图中的edit,如下所示:

<%= form_for(@project) do |f| %>