使用 Devise 删除附件(头像)



在我的registrations/edit.html.erb视图文件中,我想添加一个链接来删除当前头像(如果已附加(。我最终得到了这样的东西:

<% if current_user.avatar.attached? %>
<%= link_to "Remove avatar", { action: :remove_avatar }, method: :put %>
<% end %>

在自定义registrations_controller(继承自Devise::RegistrationsController(中,我定义了一个方法:remove_avatar

def remove_avatar
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
resource.avatar.purge_later
end

但是我遇到了此错误,这可能是由于缺少路由设置引起的。

没有路由匹配 {:action=>"remove_avatar", :controller=>"registrations", :locale=>:ru}

我该怎么做才能link_to这种方法?谢谢。

可能你需要这样的东西

put "remove_avatar", to: "registrations#remove_avatar"

在您的 routes.rb 文件中。

也许由于您的目录结构,它会变得比这更混乱,但它应该可以工作

最新更新