Ruby on Rails:字段不在编辑页面上保存数据

时间:2012-10-08 09:55:49

标签: ruby-on-rails ruby controller action edit

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

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

我的技术编辑视图:

<%= 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)

UPDATE2

PARAMS:

{"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"}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您已在:multiple => true中设置collection_select