1

我有一个应用程序,用户可以在其中提交一个新项目。提交后,他们可以选择编辑刚刚提交的项目。单击编辑时,它会再次弹出表单,并显示之前输入的所有数据。

我的问题在于允许用户选择许多技术的领域之一。它链接到另一个名为的表,该表保存表和表Projecttechnol之间的链接。当用户单击编辑按钮时,该项目所拥有的所有技术都不会显示出来。如果用户点击一项新技术并保存,则新技术保存,旧的仍然存在。TechnolProject

我对技术的编辑视图:

<%= fields_for(@project_technol) do |ab| %>
  <%= ab.label "Choose Technologies"%> </br>
  <%= collection_select( :technols, :id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
<% end %>

我的项目控制器:具有新的、编辑、创建和更新操作

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 edit
    @project = Project.find(params[:id])


@project_technol = @project.projecttechnols.build


  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?
    @project.role = params[:new_role] unless params[:new_role].blank?

    params[:technols][:id].each do |tech|


    if !tech.empty?

    @project_technol = @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

def update
    @project = Project.find(params[:id])
    @project.client = params[:new_client] unless params[:new_client].blank?
    @technol.tech = params[:new_tech] unless params[:new_tech].blank?
    @project.role = params[:new_role] unless params[:new_role].blank?

     params[:technols][:id].each do |tech|

     if !tech.empty?
     @project_technol = @project.projecttechnols.build(:technol_id => tech) 

  end
  end
    respond_to do |format|
      if @project.update_attributes(params[:project])
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

我是rails的新手,所以任何帮助都将不胜感激,在此先感谢。

更新:这是我的项目模型

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, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech , :technols, :technol_tokens

has_many :projecttechnols
has_many :technols, :through => :projecttechnols

accepts_nested_attributes_for(:technols)

更新2

参数:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"+Fdh61iVz2IqZ+bNQz9SIa5ly7nNXaiCEr28hVWUxpM=",
 "project"=>{"project_name"=>"Beverly",
 "status"=>"Completed",
 "client"=>"Qtjpa",
 "business_div"=>"Ecbly",
 "project_owner"=>"Qpzrv",
 "start_date"=>"2012-10-15",
 "edited_date"=>"2012-10-08",
 "projecttechnols"=>{"id"=>["",
 "41"]},
 "role"=>"Ozeoe",
 "industry"=>"Wjfiq",
 "summary"=>"Eypriw lorem ipsum dolor sit amet,
 consectetur adipisicing elit,
 sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
 "lessons_learned"=>"Kbjuhy lorem ipsum dolor sit amet,
 consectetur adipisicing elit,
 sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
 "customer_benefits"=>"Fsonlw lorem ipsum dolor sit amet,
 consectetur adipisicing elit,
 sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",,
 "keywords"=>"Bwdtm"},
 "new_client"=>"",
 "new_business_div"=>"",
 "new_project_owner"=>"",
 "new_role"=>"",
 "new_industry"=>"",
 "commit"=>"Save Edit",
 "id"=>"103"}
4

2 回答 2

0
<%= fields_for(@project_technol) do |ab| %>
  <%= ab.label "Choose Technologies"%> </br>
  <%= collection_select( :technols, :id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
<% end %>

应该是这样的:

<%= f.fields_for(:projecttechnols) do |ab| %>
  <%= ab.label "Choose Technologies"%> </br>
  <%= ab.collection_select(:id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
<% end %>

但是,f应替换为您用于 form_for 块的参数。喜欢:

form_for(@project) do |f|

还要确保您Project包含与嵌套模型的正确关系,并且它具有accepts_nested_attributes_for

于 2012-10-08T10:16:20.657 回答
0

您已:multiple => truecollection_select.

于 2012-10-08T10:20:01.607 回答