我的模型中有多个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 endHowever, 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 endI 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 endJust 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 endYou 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 endNote 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更多推荐
发布评论