有人能帮助我理解如何正确地写下这个案例陈述吗?它不起作用,作为一个傻瓜,我不知道如何解决它:

def hide_link?(link, mailing)
  case link
  when 'edit' &&  ['sent', 'sending', 'archived'].include?(mailing.status)
    return true
  when 'send_schedule' &&  ['sent', 'sending', 'archived'].include?(mailing.status)
    return true
  when 'archive' && ['archived'].include?(mailing.status)
    puts "I should be in here"
    return true
  else 'dashboard' && ['sending', 'draft'].include?(mailing.status)
    return true
  end
end

基本上,当链接符合某些条件时,我希望返回true。

最佳答案

我相信如果link与这些标准不匹配,那么方法应该返回false因此:

def hide_link?(link, mailing)
  case link
  when 'edit'
    ['sent', 'sending', 'archived'].include?(mailing.status)
  when 'send_schedule'
    ['sent', 'sending', 'archived'].include?(mailing.status)
  when 'archive'
    puts "I should be in here"
    ['archived'].include?(mailing.status)
  when 'dashboard'
    ['sending', 'draft'].include?(mailing.status)
  else
    false
  end
end

构造[...].include?(mailing.status)的结果truefalse将作为hide_link?方法的结果返回。

09-25 13:09