Search code examples
ruby-on-railsformshas-many-throughfields-forhtml-select

multiple select


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


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.