簡體   English   中英

Rails 4的嵌套屬性未在當前項目中添加記錄,而是在添加新項目記錄

[英]Rails 4 nested attributes not adding record to current project, instead it's adding new project records

項目更新中的記錄將被上載到新的項目記錄中。

我正在嘗試使用cocoon gem作為嵌套屬性,但是它添加了新的項目記錄,而不僅僅是在同一項目中添加了新的項目更新

我在/views/projects/show.html.haml中有此表格

= form_for @pu do |f|
  #updates
    = f.simple_fields_for :project_updates do |u|
      = render 'update_fields', f: u
  .links
    = link_to_add_association 'add updates', f, :project_updates, class: "button"
  = f.submit "Submit updates", class: "button"

然后在我的projects_controller.rb中

def show
  @pu = Project.new
end

def create
  @project = Project.new(project_params)

  respond_to do |format|
    if @project.save
      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render :show, status: :created, location: @project }
    else
      format.html { render :new }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

private

  def project_params
    params.require(:project).permit(:name, :address, :client, :budget,
      project_updates_attributes: [:updates, :notes, :done, :impact_schedule, :impact_budget, :_destroy])
  end

如果可能,我想在同一項目中不斷添加記錄

編輯,添加編輯和更新

def edit
end

def update

  respond_to do |format|
    if @project.update(project_params)
      format.html { redirect_to @project, notice: 'Project was successfully updated.' }
      format.json { render :show, status: :ok, location: @project }
    else
      format.html { render :edit }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

只是標准的編輯和更新支架。

仍然存在在顯示頁面(項目顯示頁面)內應用嵌套屬性(窗體)的問題,並且當嘗試在項目中添加新任務時,它會添加新項目記錄並將該任務包括在該新項目中,而不是更新當前項目中的任務。 如果可以幫助重寫我所修復的內容,將不勝感激。

編輯:

根據幫助,結果如下:

在此處輸入圖片說明 在此處輸入圖片說明

添加了狀態1(單擊“提交更新”),但是,當我刷新頁面時,將顯示“狀態1”輸入字段,然后當我嘗試添加另一個更新(狀態2)時,將填充原始狀態1(除了新更新),因此將不斷添加以前添加的記錄。 我只希望此顯示頁面僅將新更新添加到記錄中,而不在輸入字段中顯示現有記錄。

編輯2:

顯示頁面中的新表格,必須按照以下建議的答案消除url ,因為它沒有給我路由錯誤

= simple_form_for @project do |f|
  #updates
    = f.simple_fields_for :project_updates do |u|
      = render 'project_update_fields', f: u
  .links
    = link_to_add_association 'add updates', f, :project_updates, class: "button"
  = f.submit "Submit updates", class: "button"

_project_update_fields.haml部分

.nested-fields
  = f.input :updates, placeholder: "Updates", label: false
  .small-2.column
    = f.input :impact_budget, as: :boolean
  .small-2.column  
    = f.input :impact_schedule
  .small-12.column
    = link_to_remove_association "remove task", f, class: "button"

幾乎沒有要處理的問題。

首先也是最重要的是:

def show
  # instantiating new project_update is not even needed, nested_attributes will do that for you
  @pu = Project.new
end

為了能夠更新現有項目,您需要執行以下操作:

# change the show so that it finds the project which are at now:

def show
  # find a project we want to add updates to
  @project = Project.find(params[:id])
end

# edit the form a bit
= form_for(@project, url: projects_path(@pu)) do |f| # <===, not nessecary though. Should work as you have it as well
  #updates
    = f.simple_fields_for :project_updates do |u|
      = render 'update_fields', f: u

編輯

要僅創建新更新(而不在項目的編輯表單上顯示現有更新),請執行以下操作:

# making sure, that simple_fields_for are only displayed for new update object's creation
- if f.object.new_record?
  .nested-fields
    = f.input :updates, placeholder: "Updates", label: false
    .small-2.column
      = f.input :impact_budget, as: :boolean
    .small-2.column  
      = f.input :impact_schedule
    .small-12.column
      = link_to_remove_association "remove task", f, class: "button"

首先,您需要將操作拆分為創建和更新。 這樣,您可以更新現有記錄。

您的更新操作將非常相似,但是

@project = Project.new(...)

它會像

@project = Project.find(params[:project_id])

另外,您將需要調用@project.update_attributes(project_params)而不是@project.save祝您好運。 如果您有任何問題,請詢問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM