简体   繁体   English

Rails 4的嵌套属性未在当前项目中添加记录,而是在添加新项目记录

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

And the records from Project Updates are being uploaded to the new Project record. 项目更新中的记录将被上载到新的项目记录中。

I'm trying to use cocoon gem for nested attributes, but its adding new project record, and not just adding new project updates within the same project 我正在尝试使用cocoon gem作为嵌套属性,但是它添加了新的项目记录,而不仅仅是在同一项目中添加了新的项目更新

I have the form in my /views/projects/show.html.haml 我在/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"

Then in my projects_controller.rb 然后在我的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

I would like to continuously add records in the same project, if possible 如果可能,我想在同一项目中不断添加记录

EDIT, adding edit and update 编辑,添加编辑和更新

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

Just standard edit and update scaffold. 只是标准的编辑和更新支架。

Still having the issue of applying a nested attribute (form) inside a show page (project show page), and when trying to add new tasks in the project, it adds a new project record and includes the tasks in that new project, instead of updating the tasks in the current project. 仍然存在在显示页面(项目显示页面)内应用嵌套属性(窗体)的问题,并且当尝试在项目中添加新任务时,它会添加新项目记录并将该任务包括在该新项目中,而不是更新当前项目中的任务。 If its possible to help rewrite what I had, to fix, would be appreciated. 如果可以帮助重写我所修复的内容,将不胜感激。

EDIT: 编辑:

Per help, this is the result: 根据帮助,结果如下:

在此处输入图片说明 在此处输入图片说明

Status 1 was added (clicked on Submit updates), however, when I refresh the page, Status 1 input field shows up, and then when I tried to add another update (Status 2), the original Status 1 gets populated (in addition to the new updates), so there's going to be a continuous addition of previously added records. 添加了状态1(单击“提交更新”),但是,当我刷新页面时,将显示“状态1”输入字段,然后当我尝试添加另一个更新(状态2)时,将填充原始状态1(除了新更新),因此将不断添加以前添加的记录。 I just want this show page to only add new updates into the records, and not show existing records in input fields. 我只希望此显示页面仅将新更新添加到记录中,而不在输入字段中显示现有记录。

EDIT 2: 编辑2:

New form in show page, had to eliminate url per suggested answer below because it was giving me no route error 显示页面中的新表格,必须按照以下建议的答案消除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 partial _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"

There are few issues to work on. 几乎没有要处理的问题。

First and the most important is here: 首先也是最重要的是:

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

To be able to update existing project, you need to do the following: 为了能够更新现有项目,您需要执行以下操作:

# 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

EDIT 编辑

To only create new updates (and not display existing ones on the project's edit form) you would do the following: 要仅创建新更新(而不在项目的编辑表单上显示现有更新),请执行以下操作:

# 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"

First off you need to split your actions into create and update. 首先,您需要将操作拆分为创建和更新。 That way you can update an existing record. 这样,您可以更新现有记录。

Your update action will be very similar, but instead of 您的更新操作将非常相似,但是

@project = Project.new(...)

It will be something like 它会像

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

Also, you will want to call @project.update_attributes(project_params) instead of @project.save Good luck. 另外,您将需要调用@project.update_attributes(project_params)而不是@project.save祝您好运。 If you have any questions please ask. 如果您有任何问题,请询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM