Rails通过关联删除has_many
Rails delete has_many through association
我有 has_many 通过与用户、附件和表单模型的关联,我只想删除关联,而不是附件。我写了名为 "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 %>
我有附件的资源路由,我有附件的 destroy 方法来删除元素。我需要 sil 方法的查看和路由帮助
试试这个:
resources :attachments do
member do
delete 'sil'
end
end
这样的路由助手:
sil_attachment_path(id: attachmentid, user_id: userid)
我有 has_many 通过与用户、附件和表单模型的关联,我只想删除关联,而不是附件。我写了名为 "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 %>
我有附件的资源路由,我有附件的 destroy 方法来删除元素。我需要 sil 方法的查看和路由帮助
试试这个:
resources :attachments do
member do
delete 'sil'
end
end
这样的路由助手:
sil_attachment_path(id: attachmentid, user_id: userid)