基于params的Carrierwave删除方法(Carrierwave remove method based on params)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
基于params的Carrierwave删除方法(Carrierwave remove method based on params)

我的模型中有多个Carrierwave附件字段,我希望能够在我的节目视图中删除。 由于有多个字段,我将参数传递给我的控制器方法,因此它知道要删除哪个文件:

def remove_attachment post = Post.find(params[:id]) post["remove_#{params[:attachment]}!"] post.save redirect_to post end

但是,以下行不起作用post["remove_#{params[:attachment]}!"] ? 不确定我在哪里错了?

我已经测试了以下哪些有效:

def remove_attachment post = Post.find(params[:id]) post.remove_compliance_guide! # one of the attachment fields post.save redirect_to post end

我意识到我可以在下面做,但我认为我的第一个解决方案是更清洁。

def remove_attachment post = Bid.find(params[:id]) if params[:attachment] == 'compliance_guide' post.remove_compliance_guide! elsif params[:attachment] == 'compliance_other' post.remove_compliance_other! elsif params[:attachment] == 'compliance_agreement' post.remove_compliance_agreement! end post.save redirect_to post end

以防我在其他地方犯了错误:

路线: post 'posts/:id/remove_attachment', to: 'posts#remove_attachment'

查看链接: link_to 'Remove', { controller: 'post', action: "remove_attachment", attachment: 'compliance_guide', id: @post.id }, method: 'post', class: "file-remove right"

I have multiple Carrierwave attachment fields in my model which I would like to be able remove in my show view. As there are multiple fields I pass a parameter to my controller method so it knows which file to remove:

def remove_attachment post = Post.find(params[:id]) post["remove_#{params[:attachment]}!"] post.save redirect_to post end

However, the following line does not work post["remove_#{params[:attachment]}!"]? Not sure where I am going wrong here?

I've tested the following which does work:

def remove_attachment post = Post.find(params[:id]) post.remove_compliance_guide! # one of the attachment fields post.save redirect_to post end

I realise that I could do the below but I think my first solution is cleaner.

def remove_attachment post = Bid.find(params[:id]) if params[:attachment] == 'compliance_guide' post.remove_compliance_guide! elsif params[:attachment] == 'compliance_other' post.remove_compliance_other! elsif params[:attachment] == 'compliance_agreement' post.remove_compliance_agreement! end post.save redirect_to post end

Just in case I've made a mistake somewhere else:

route: post 'posts/:id/remove_attachment', to: 'posts#remove_attachment'

view link: link_to 'Remove', { controller: 'post', action: "remove_attachment", attachment: 'compliance_guide', id: @post.id }, method: 'post', class: "file-remove right"

最满意答案

您可以使用send通过将其名称作为字符串传递来调用方法:

def remove_attachment post = Post.find(params[:id]) post.send("remove_#{params[:attachment]}!") post.save redirect_to post end

请注意,如果附件不存在,将引发NoMethodError 。 你可以使用以下方法解决这个问题:

if post.respond_to?("remove_#{params[:attachment]}!") post.send("remove_#{params[:attachment]}!") post.save end

You can use send to invoke a method by passing its name as a string:

def remove_attachment post = Post.find(params[:id]) post.send("remove_#{params[:attachment]}!") post.save redirect_to post end

Note that a NoMethodError will be raised if the attachment doesn't exist. You could work around that using something like:

if post.respond_to?("remove_#{params[:attachment]}!") post.send("remove_#{params[:attachment]}!") post.save end

更多推荐

本文发布于:2023-04-17 08:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/796ffdbbfecf2317ca5a929e6daf3958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Carrierwave   params   based   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!