我在尝试让我的编辑和更新操作适用于我的应用时遇到了真正的问题。我想编辑与项目链接的技术。
def edit
@project = Project.find(params[:id])
@project_technol = @project.projecttechnols.build
end
def update
@project = Project.find(params[:id])
@project.client = params[:new_client] unless params[:new_client].blank?
@project.role = params[:new_role] unless params[:new_role].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
params[:technols][:id].each do |tech|
if !tech.empty?
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
项目型号:
class Project < ActiveRecord::Base
attr_accessible :fullname, :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benefits, :edited_date, :end_date, :entry_date,:industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech , :technols,
has_many :projecttechnols
has_many :technols, :through => :projecttechnols
accepts_nested_attributes_for(:technols)
当我进入编辑表单时,之前选择的技术没有在集合选择中选择。
编辑表格:
<%= f.fields_for(@project_technol) do |t| %>
<%= t.label "Choose Technologies"%> </br>
<%= t.collection_select(:id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
<% end %>
<% @project.technols.each do |t| %>
<li><%= t.tech %> <%= link_to "Details", technol_path(t), method: :delete, %></li>
<% end %>
我可以看到使用@project.technols.each do |t|
循环有哪些技术,因此可以找到它们,但它们没有显示在 collection_select 中。请有人指出我正确的方向。我是 Rails 的新手,所以它可能是我不理解的每一个简单的东西。提前致谢。