加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails – 混合’发送’和’尝试’

发布时间:2020-12-17 03:24:27 所属栏目:百科 来源:网络整理
导读:我有一个可能是零的对象.在下一行中,我根据某些条件动态获取对象的任何一个参数.我正在使用带有if else块的object.try().现在我想使用send()以便我可以实现一些有效的代码. form_name = 'parent' ( received from argument )mes_record = Mark.first ( this
我有一个可能是零的对象.在下一行中,我根据某些条件动态获取对象的任何一个参数.我正在使用带有if else块的object.try().现在我想使用send()以便我可以实现一些有效的代码.

form_name = 'parent' ( received from argument )
mes_record = Mark.first ( this can be nil )

if x == condition_1
    mes_record.parent
elsif x == condition_2
    mes_record.brother
elsif x == condition_3
    mes_record.sister
else
    mes_record.father
end

现在我想避免使用if else梯子并使用send.

if mes_record.present?
      mes_record.send("#{form}")
 else
      mes_record = 'not available'
 end

我正在寻找类似尝试的东西.

mes_record.try("dynamically substitute the attribute name here.") || 'not available'

有什么帮助?!

解决方法

尝试了一个小时后,我找到了解决方案.

帽子到Rails尝试方法.

mes_record.try(:send,“#{form}”)|| ‘无法使用’

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读