Fdwillis
Fdwillis

Reputation: 1049

Rails Cocoon Delete/Destroy Single Instance

I am using Cocoon gem to associate resume fields to one resume, and each user has ONE resume and have everything working the correct way via cocoon documentation. However, I would like the ability for users to be able to edit resume fields/nested models directly on the profile page without being redirected to the cocoon nested form page. Further more I would like users to have the ability to add and or delete a SINGLE field/entry. Is there any way at all to accomplish this?

In my console I have been able to successfully delete an entire resume nested model using

User.last.resume.resume_edus.destroy_all

Where 'resume.edus' is the nested model inside of resume complete with 3 other 'text_fields'. But as stated I would only like to be able to edit/delete a single instance of resume_edus. Any ideas?

User.rb

has_one :profile
has_one :resume

Resume.rb

belongs_to :user
has_many :resume_edus
accepts_nested_attributes_for :resume_edus,
                               reject_if: :all_blank,
                               allow_destroy: true

Resume_edu

belongs_to :resume

Resume Controller

params.require(:resume).permit(:user_id, :cover,
                                resume_edus_attributes: 
                                 [:id, :title, :date, :description, :_destroy])

Upvotes: 0

Views: 669

Answers (2)

anas1590
anas1590

Reputation: 1

accepts_nested_attributes_for :order_line_items, allow_destroy: true
<tr class="nested-fields">
<%= f.hidden_field :_destroy %>
<%= link_to_remove_association "Delete", f %>
</tr>

Add _destroy in controller params:

order_line_items_attributes: %i[id product_id quantity price discount total _destroy]

Upvotes: 0

Fdwillis
Fdwillis

Reputation: 1049

Figured out the answer! I passed the resume_edus 'ID' in the delete action

<%= link_to "Delete", resume_path(r.id), method: :delete %>

Then was able to search for said ID in the current users resume_edu modal to specifically isolate and delete it without deleting the other instances of the model!

def destroy
  @instance = current_user.resume.resume_edus.find(params[:id])
  debugger
  @instance.destroy
  flash[:notice] = "Resume Field Was Deleted"
  redirect_to profile_path(current_user)
end

Upvotes: 1

Related Questions