shesso

Joined

80 Experience
0 Lessons Completed
0 Questions Solved

Activity

Error seems to be fixed by changing the code to
  def clone
    @project = Project.find(params[:id]).amoeba_dup
    @project.save
    render 'new'
  end

Thanks for your help, Jacob!

Adding .save creates the project and connects copies of the tasks!

but I get a new error stopping the form view being loaded
ActionView::Template::Error (undefined method `model_name' for true:TrueClass):
Sorry. I've been adding and removing parts in the amoeba code to see if anything changes.
Added the include association back in, but the tasks still do not show up.
I'm fairly new to Rails and still learning. I recently stumbled upon a problem that I'm sure there is an easy way to fix it, but I haven't figured it out yet.

In the application I'm trying to create there are projects. Each project has multiple tasks. Once one project is set up with all tasks I want to use this as a template for creating new projects (with the same set of tasks). Some of the code contains snippets from episodes here on GoRails (the tasks are sortable etc). Cocoon is used for nested forms.

I have been trying to create duplicates of the template with and without deep_clonable and amoeba, but they are not picking up the associated tasks in the forms.
I'm pretty much at a stand still atm. 

class Project < ApplicationRecord

  belongs_to :user

  has_many :tasks, inverse_of: :project, dependent: :destroy
  accepts_nested_attributes_for :tasks, reject_if: :all_blank, allow_destroy: true

  amoeba do
    enable
    prepend :title => "Copy of "
  end

end
(The amoeba part is not present when not in use in the controller.)
class Task < ApplicationRecord

  belongs_to :project, touch: true

end

Try 1
from  projects_conroller.rb - An online answer which seem to work for some.
  def clone
    @project = Project.find(params[:id])
    @project = Project.new(@project.attributes)
    render 'new'
  end
Tasks show up, but throws an error on save.
Completed 401 Unauthorized in 19ms (ActiveRecord: 3.7ms)
ActiveRecord::RecordNotFound (Couldn't find Task with ID=1 for Project with ID=):  
app/controllers/projects_controller.rb:32:in `create'

Try 2 - deep_clonable
  def clone
    @project = Project.find(params[:id]).deep_clone include: :tasks, except: [ { task: [:status ] } ], only: :title    
    render 'new'
  end
Tasks are not showing up in form. Even when used in its simplest form with only include:

Try 3 - amoeba
 def clone
    @project = Project.find(params[:id]).amoeba_dup    
    render 'new'
  end
Tasks are not showing up in form. 

I'm guessing this may be solved with a single line of code by more experienced programmers. 
Any help is greatly appreciated.
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.