Ramazan Zor
Ramazan Zor

Reputation: 209

Rails delete has_many through association

I have has_many through association with User, Attachment and Form model, I want to delete associaton only, not the attachment. I wrote delete method named "sil"

user.rb

  has_many :forms
  has_many :attachments, through: :forms

attachment.rb

has_many :forms
has_many :users, through: :forms

form.rb

 belongs_to :user
 belongs_to :attachment

sil method

def sil # remove the product from user
 @user = User.find(params[:id])
 attachment = Attachment.find(params[:attachid])
 @user.attachments.delete(attachment)
 redirect_to user_path(@user.id)
end

view

<%= button_to "Sil",attach ,method: "delete",:controller => "attachments", :action => "sil" , :attachid =>attach.id %>

I have resources routes for attachments and I have destroy method for attachments to delete an element. I need help for view and route for sil method

Upvotes: 0

Views: 601

Answers (1)

dipak gupta
dipak gupta

Reputation: 366

Try this:

resources :attachments do
  member do
    delete 'sil'
  end
end

routing helper like this:

sil_attachment_path(id: attachmentid, user_id: userid)

Upvotes: 1

Related Questions