小编Mic*_*Fin的帖子

Ruby迭代变量,除非它是零

如果变量不是nil,我想在.html.erb中以干净的方式循环遍历变量.

我想要执行以下操作,但如果@family为零则不执行.

<% @family.children.each.with_index(1) do |family_member, index| %>
    // HTML HERE
<% end %>
Run Code Online (Sandbox Code Playgroud)

我试图避免做这样的事情

<% if @family %>
   <% @family.children.each.with_index(1) do |family_member, index| %>
       // HTML HERE
   <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

特别是试图避免需要

<% if @family && @family.children %>
      <% @family.children.each.with_index(1) do |family_member, index| %>
          // HTML HERE
      <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

ruby ruby-on-rails erb ruby-on-rails-4

4
推荐指数
2
解决办法
2742
查看次数

使用出生日期的红宝石年龄方法,包括月份

我的用户模型上有一个方法来计算用户的年龄并返回一个人类可读的字符串.我的用户可以在1个月及以上之间,因此返回的字符串会有所不同,具体取决于该人是"2个月大"还是"1岁"或"2岁3个月".

我已经回顾了几个SO帖子来解决这个问题.有什么我想念的吗?闰年?谢谢!

def age
    dob = self.date_of_birth

    # if a date of birth is not nil
    if dob != nil 

      # get current date  
      now = Date.current

      # has person had their birthday yet this year
      had_birthday = ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? true : false) 

      # if yes then subtract this year from birthday year, if not then also subtract 1 to get how many full years old they are
      years = …
Run Code Online (Sandbox Code Playgroud)

ruby datetime date date-of-birth ruby-on-rails-4

4
推荐指数
2
解决办法
3196
查看次数

第一个git aws.push到弹性beanstalk时出错

我是Elastic Beanstalk的新手,我正在尝试推出Rails 4应用程序.我已经按照http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-reference-get-started.html这里的指南进行了操作但是当我去的时候

git aws.push
Run Code Online (Sandbox Code Playgroud)

它告诉我,我的更新有错误.

[Instance: i-a6482b8b Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: Error occurred during build: Command hooks failed .
Run Code Online (Sandbox Code Playgroud)

ebs日志只是挤满了我不熟悉的许多令人困惑的项目.很难说分享什么,但这两个方面都很突出

  1. 很多不在这里,所以这不是一个好兆头.还调用了一堆令人困惑的python文件,因为我应该运行运行Ruby 2.0(Passenger Standalone)的64位Amazon Linux 2014.03 v1.0.4.

    14-09-22 05:31:28,375 [INFO]运行configSet Infra-WriteApplication2 2014-09-22 05:31:28,376 [INFO]运行配置Infra-WriteApplication2 2014-09-22 05:31:28,376 [DEBUG]否指定包裹2014-09-22 05:31:28,376 [DEBUG]未指定群组2014-09-22 05:31:28,377 [DEBUG]未指定用户2014-09-22 05:31:28,377 [DEBUG]未指定来源2014-09-22 05:31:28,377 [DEBUG]将内容写入/ opt/elasticbeanstalk/bin/download_source_bundle 2014-09-22 05:31:28,377 [DEBUG]从https://s3.amazonaws.com/检索内容elasticbeanstalk-env-resources-us-east-1/eb_patching_resources/download_source_bundle.py 2014-09-22 05:31:28,624 [DEBUG]/opt/elasticbeanstalk/bin/download_source_bundle设置模式为000750 2014-09-22 05: 31:28,625 [DEBUG]为/ opt/elasticbeanstalk/bin/download_source_bundle设置所有者0和组0 2014-09-22 05:31:28,625 [DEBUG]运行命令01downloadVersion 2014-09-22 05:31:28,626 [DEBUG]没有测试命令01downloadVersion 2014-09-22 05:31:29,3 54 [INFO]命令01downloadVersion成功2014-09-22 …

git deployment amazon-web-services ruby-on-rails-4 amazon-elastic-beanstalk

3
推荐指数
1
解决办法
3538
查看次数

在javascript数组"myArray [1,3]"中访问值时传递2个值

我意识到这是不正确的语法,但为什么JavaScript会忽略第一个数字并给出响应而不是破坏?

let myArray = [1,2,3,4]

myArray[0,1] // 2
myArray[1,3] // 4
myArray[3,0] // 1 
Run Code Online (Sandbox Code Playgroud)

javascript arrays

2
推荐指数
1
解决办法
63
查看次数

使用Devise 3和Rails 4将图像添加到确认电子邮件

我想使用Devise 3和Rails 4将图像添加到我的确认电子邮件中。

我试过了...

Views / Devise / Mailer / confirmation_instructions.html.erb

<%= image_tag("my_image.png") %>
Run Code Online (Sandbox Code Playgroud)

...但是图像显示为默认的占位符问号。

我应该如何正确访问资产?

image devise asset-pipeline devise-confirmable ruby-on-rails-4

1
推荐指数
1
解决办法
1106
查看次数

使用正则表达式在字符串之间找到一些东西并切换它们

我在字符串"##/##/#### ##:## ## ###"中有以下模式##/##/####.例如,"11/22/3333美国东部时间上午11:22"希望将11和22切换为22/11/3333.我是了解正则表达式的新手.谢谢.

ruby regex datetime datetime-format ruby-on-rails-3

1
推荐指数
1
解决办法
47
查看次数

在jquery事件中使用Javascript"this"

如何在Jquery更改函数中正确调用functionOne?"this"不再引用该对象.

NameSpace.object = {
  functionOne: function(){
     // stuff
  },
  functionTwo: function(){
     this.functionOne();  // this works correctly
  },
  functionThree: function(){
    $('input').change(function(){
       if ( $(this).val() > 1) {
         this.functionOne(); // this is no longer calling function one 
       }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript jquery function this

1
推荐指数
1
解决办法
61
查看次数