Rails通过关联

时间:2015-08-28 12:15:20

标签: ruby-on-rails associations sql-delete has-many-through

我通过与User,Attachment和Form模型的关联而拥有has_many,我只想删除associaton,而不是附件。我写了名为" 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方法

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
  

视图

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

我有附件的资源路径,我有删除元素的附件的破坏方法。我需要帮助查看和路由sil方法

1 个答案:

答案 0 :(得分:1)

试试这个:

resources :attachments do
  member do
    delete 'sil'
  end
end
像这样路由助手:

sil_attachment_path(id: attachmentid, user_id: userid)