项目更新中的记录正在上传到新的项目记录中。
我试图将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"
答案 0 :(得分: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"
答案 1 :(得分:0)
首先,您需要将操作拆分为创建和更新。这样您就可以更新现有记录。
您的更新操作将非常相似,而不是
@project = Project.new(...)
这将是
@project = Project.find(params[:project_id])
另外,您需要拨打@project.update_attributes(project_params)
而不是@project.save
祝好运。如果您有任何疑问,请询问。