Question

I am trying to create a form that allows to assign multiple projectmembers to a project. (User and Project model being related through UserProject )

here is my view:

<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>

i have put the projectmember tag all over the place but i can't figure it out how to save this field projectmember into my db projects and 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

and

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

After creating one instance, the command using the console : @project=Project.all gives : Project id: 55, projectname: "fdfd", projectdescription: "fdfd", projectheader: "5", projectmember: nil, projecttypename: "dffd">]

Was it helpful?

Solution

Assuming your associations are something like this:

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

...for your project form, use the following helper with the "name" option (:name=>'project[user_ids][]') to pass the selected ids to your controller:

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

That will give you the selected ids in the params and then you'll have to hook up your controller actions to assign them. For example:

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

In order to get the selected items to re-select when you render the edit form, I'm assuming you'd have to add that as an argument to the options_for_select method. Not exactly sure about that. An alternative to doing all of the above would be to use checkboxes to select the users since the checked could be set for each assigned user when the form renders. Good luck.

OTHER TIPS

my model was indeed like that :

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

i have needed to change in my view ( else Can't mass-assign protected attributes):

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

by

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

and in my controller:

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)

i got now after creation :

@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">,

which i am happy about :)

still need to :

delete automatically the project/user couple if the project is deleted

show on the screen the corresponding member names rather than an id number...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top