我有一个允许用户创建新项目的应用程序。在创建新项目表单中,对于每个字段,他们可以选择创建新数据以进入该字段,或从以前的项目中选择数据,例如,输入新客户名称,或从项目中选择现有客户以前输入过的数据库。
现在我已经建立了技术和项目之间的关系表,允许用户选择多种技术来配合一个项目。
我的问题是当我尝试允许他们提交更多技术时,如果它们不存在于 collection_select 中。
这是我的新观点:
<%= stylesheet_link_tag "form" %>
<%= form_for(@project) do |f| %>
<div class="client">
<%= label_tag :new_client, "Client" %><br/>
<%= text_field_tag :new_client, nil, :maxlength => 30 %>
Or
<%= f.select( :client, Project.all.map {|p| [p.client]}.uniq, :prompt => "Select a previous Client") %>
</div>
<%= fields_for(@project_technol) do |ab| %>
<%= text_field_tag :tech, nil, :maxlength => %>
<%= ab.label "All Tech"%> </br>
<%= collection_select( :technols, :id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
</div>
<% end %>
<div class="create_button">
<div class="actions">
<%= f.submit "Save New Project", :class => "button", :confirm => "Are you sure you want to save the new project?" %>
</div>
</div>
</div>
<% end %>
<div class="back_button2">
<%= button_to "Back", projects_path , :class => "button", :method => "get" %>
</div>
这是我的创建和新操作:
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
def create
@project = Project.new(params[:project])
@project.client = params[:new_client] unless params[:new_client].blank?
@technol.tech = params[:new_tech] unless params[:new_tech].blank?
params[:technols][:id].each do |tech|
if !tech.empty?
@project.projecttechnols.build(:technol_id => tech)
end
end
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 }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
我会很感激任何帮助,我真的被这个问题困住了。我是 Rails 新手,所以在尝试提供帮助时请记住这一点。