【问题标题】:Rails 4 nested attributes not adding record to current project, instead it's adding new project recordsRails 4嵌套属性不向当前项目添加记录,而是添加新项目记录
【发布时间】:2016-02-01 23:10:34
【问题描述】:

项目更新中的记录正在上传到新的项目记录中。

我正在尝试将 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

如果可能的话,我想在同一个项目中不断添加记录

EDIT,添加编辑和更新

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"

【问题讨论】:

  • 您缺少editupdate 操作,或者您只是没有向我们展示它们?
  • @nathanvda 我更新了我的问题,它只是标准的脚手架操作

标签: ruby-on-rails ruby ruby-on-rails-4 nested-attributes cocoon-gem


【解决方案1】:

有几个问题需要解决。

首先也是最重要的在这里:

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"

【讨论】:

  • 它现在保存记录,但它还会添加以前的记录以及我正在尝试更新的新记录。当我在项目显示页面中时,它会在输入字段中显示记录,就好像我正在编辑表单一样。那有意义吗?我该如何预防?
  • @hellomello When I'm in the project show page, it shows the records in the input fields as if I'm editing the form.,想一想 - 你在显示页面上,你正在更新现有记录是合乎逻辑的。我可以请您写下您关心的问题吗?因为不幸的是我不明白它。我以为您希望表单能够正常工作并将新的project_updates 添加到项目页面上的现有项目中。你说它有效,所以现在我想知道还有什么要做的,所以我可以进一步帮助你:)
  • 嗨!非常感谢帮忙。我已经更新了我的 OP 以截图我的问题。希望这是有道理的!
  • @hellomello 很简单,就是部分代码的问题。请发布部分内容,我将向您展示如何仅添加新更新:)
  • 谢谢!我更新了我的 OP。但是你会看到我不得不对你建议的表格进行一些修改。
【解决方案2】:

首先,您需要将操作拆分为创建和更新。这样您就可以更新现有记录。

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

@project = Project.new(...)

会是这样的

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

另外,您需要调用 @project.update_attributes(project_params) 而不是 @project.save 祝你好运。如果您有任何问题,请提出。

【讨论】:

  • 我怎么要把Project.new(..)改成Project.find(params[:project_id])?我正在尝试为现有项目显示页面创建一个嵌套属性,用户可以在其中更新项目中的项目状态。另外,create 动作中的@project.save 是在我创建新项目时使用的,不是吗?
  • 当然,但您也想更新内容,对吧?为此,您需要一个名为 update 的新操作,并且在该操作中您应该替换我所说的方法。您编写的代码没有任何问题,但它每次都会创建新项目。如果您想更新项目,您需要按照我给您的说明执行新操作。对不起,如果我不够清楚。
  • @pu = Project.new 怎么样?因为显示页面中的这个表格是action="/projects"。也许我需要改变这个?因为如果我改变原来的脚手架项目,它会影响我创建新项目的时间,对吧?
  • 新页面应该是 /projects 的帖子。您还应该有一个 edit 操作,带有一个编辑视图,带有一个到 /projects 的 PATCH 表单。在 Rails 中,这与说 edit_project_path(PROJECT_ID) 相同
  • 所以我想这是我的问题。我不是想发布一个新项目,我只是想在项目中添加新任务。我在项目的展示页面中,有一个只添加更新的表单。如果它到/projects 不是创建一个新项目,而不是“项目更新”(让我们调用任务)?对不起,我的 n00b 问题,我正在努力理解并希望让它发挥作用。也许我在更新和任务之间混淆了你
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多