2015-08-28 122 views
0

我有has_many通过与用户,附件和表单模型关联,我只想删除关联,而不是附件。我写了一个名为 “SIL”Rails通过关联删除has_many

user.rb

has_many :forms 
    has_many :attachments, through: :forms 

attachment.rb

has_many :forms 
has_many :users, through: :forms 

form.rb

01删除方法
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

试试这个:

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

路由助手这样的:

sil_attachment_path(id: attachmentid, user_id: userid) 
+0

我用delete“SIL”,而不是得到的,我必须给像你这样的路径参与了吗? –

+0

你在行动中发现用户和附件,然后得到你需要通过链接传递params的记录,和我一样。 –

相关问题