简体   繁体   中英

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

I have the form in my /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

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. I just want this show page to only add new updates into the records, and not show existing records in input fields.

EDIT 2:

New form in show page, had to eliminate url per suggested answer below because it was giving me no route error

= 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

.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. If you have any questions please ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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