繁体   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