多选

时间:2012-06-06 16:47:21

标签: ruby-on-rails forms has-many-through fields-for html-select

我正在尝试创建一个允许将多个项目成员分配给项目的表单。 (用户和项目模型通过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标记放在所有地方,但我无法弄清楚如何将此字段项目成员保存到我的db项目和user_projects !! ??

my projects_controller:

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”,项目成员:nil,projecttypename:“dffd”&gt;]

2 个答案:

答案 0 :(得分:1)

假设您的关联是这样的:

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

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

<%= 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方法中。不完全确定。执行上述所有操作的替代方法是使用复选框来选择用户,因为在表单呈现时可以为每个分配的用户设置选中。祝好运。

答案 1 :(得分: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号...