我正在尝试创建一个表单,允许将多个项目成员分配给一个项目。(用户和项目模型通过 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 ">]