1

我正在尝试创建一个表单,允许将多个项目成员分配给一个项目。(用户和项目模型通过 UserProject 关联)

这是我的看法:

<div class="field">
<%= fields_for :projectmember do |u| %> 
  <%= u.label :projectmember %><br />
  <%= select_tag :projectmember, options_for_select(User.all.collect {|u| [u.id, u.lastname]}, :projectmember),:multiple => true, :prompt => 'Select Person' %>

<% end %>
  </div>

我已经把projectmember标签到处都是,但我不知道如何将这个字段projectmember保存到我的数据库项目和user_projects中!!??

我的项目控制器:

def new
@project = Project.new
@user_project=UserProject.new
@user=User.all    
@user_lastnames = User.all.collect do |u| 
  u.lastname
end

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @project }
end
end

def create
@project = Project.new(params[:project])  

respond_to do |format|
  if @project.save
    format.html { redirect_to @project, notice: 'Project was successfully created.' }
    format.json { render json: @project, status: :created, location: @project }         

    @user_project=UserProject.create(:project_id => @project.id, :user_id => @project.projectmember)       

  else
    format.html { render action: "new" }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
end

创建一个实例后,使用控制台的命令:@project=Project.all 给出:项目 id: 55, projectname: "fdfd", projectdescription: "fdfd", projectheader: "5", projectmember: nil, projecttypename: "dffd ">]

4

2 回答 2

1

假设您的关联是这样的:

class Project < ActiveRecord::Base
  has_many :project_users
  has_many :users, :through=>:project_users
end

...对于您的项目表单,使用带有“名称”选项的以下帮助程序 (:name=>'project[user_ids][]')将选定的 ids 传递给您的控制器:

<%= select_tag :user_ids, options_for_select(User.all.collect {|u| [u.lastname, u.id]}),:multiple => true, :prompt => 'Select Person', :name=>'project[user_ids][]' %>

这将为您提供参数中选定的 id,然后您必须连接控制器操作以分配它们。例如:

def create
    @project = Project.new(params[:project])  
    users = User.find(params[:project][:user_ids]) rescue []
    @project.users = users
    if @project.save
      ...
    else
      ...
    end
end

def update
    @project = Project.find(params[:id])
    users = User.find(params[:project][:user_ids]) rescue []
    @project.users = users
    if @project.update_attributes(params[:project])
      ...
    else
     ...
    end
end

为了在您呈现编辑表单时重新选择所选项目,我假设您必须将其作为参数添加到 options_for_select 方法。不完全确定。执行上述所有操作的替代方法是使用复选框来选择用户,因为可以在表单呈现时为每个分配的用户设置检查。祝你好运。

于 2012-06-06T21:15:41.820 回答
0

我的模型确实是这样的:

class Project < ActiveRecord::Base
  has_many :project_users
  has_many :users, :through=>:project_users

  attr_accessible :colourcode, :projectdescription, :user_id, :projectname, :projectheader, :projectmember, :projecttypename
end

我需要改变我的观点(否则不能批量分配受保护的属性):

    <%= select_tag :user_ids, options_for_select(User.all.collect {|u| [u.lastname, u.id]}),:multiple => true, :prompt => 'Select Person', :name=>'project[user_ids][]' %>

经过

<%= select_tag :projectmember, options_for_select(User.all.collect {|u| [u.lastname, u.id]}),:multiple => true, :prompt => 'Select Person', :name=>'project[projectmember][]' %>

在我的控制器中:

def create
@project = Project.new(params[:project])  

users = User.find(params[:project][:projectmember]) rescue []
@project.users = users    



respond_to do |format|
  if @project.save
    format.html { redirect_to @project, notice: 'Project was successfully created.' }
    format.json { render json: @project, status: :created, location: @project }         

   @user_project=UserProject.create(:project_id => @project.id, :user_id => @project.projectmember)
    @user_project=UserProject.create(:project_id => @project.id, :user_id => @project.projectheader)

创建后我现在得到了:

@p=Project.all

 Project id: 69, projectname: "test", projectdescription: "blabla", colourcode: "blue", projectheader: "5", projectmember: "---\n- '5'\n- '6'\n", projecttypename: "caucasian">]


@up=UserProject.all


UserProject id: 86, project_id: 69, user_id: 5, created_at: "2012-06-07 13:04:51", updated_at: "2012-06-07 13:04:51">

UserProject id: 87, project_id: 69, user_id: 6, created_at: "2012-06-07 13:04:51", updated_at: "2012-06-07 13:04:51">,

我很高兴:)

仍然需要:

如果项目被删除,则自动删除项目/用户对

在屏幕上显示相应的成员名称而不是 ID 号...

于 2012-06-07T13:37:51.087 回答