小编Phr*_*ogz的帖子

BigDecimal的方法缩小规模和精度

我需要一个方法,该方法在创建为变量的BigDecimal数字的范围内

例:

number1 = BigDecimal.new('154.4978')
number1 => 154.4978
Run Code Online (Sandbox Code Playgroud)

现在我想要那个数字1 = 154.49.我如何降低规模?

ruby floating-point ruby-on-rails

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

我如何使用javascript中的Rails.logger

我nside一个html.erb,我想使用记录器

<script type="text/javascript">
  $j(function(){

    $j('#AddQuestion').click(function(){
     //Send and http transaction to the server to get the address
      $j('#dialog').dialog(
        {buttons:{OK:function (event) {
                say('You clicked the ' + $(event.target).text() + ' button' );
              },  No:function (event) {
                say('You clicked the ' + $(event.target).text() + ' button' );
              }
                }
         });

      $j('#dialog').dialog('open');

    });
  });
</script>
Run Code Online (Sandbox Code Playgroud)

javascript jquery-ui ruby-on-rails-3

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

未指定id时的addEventListener()

这个叫每个顶级菜单

var arrayTop=document.getElementById("topmenu").getElementsByTagName("a");
for (i=0;i<arrayTop.length;i++){
  document.getElementById(arrayTop[i].id).addEventListener("click",topMenu,false);
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div id="topmenu">
  <a id="help" href=#><span>Help</span></a>
  <a id="frum" href=#><span>Forum</span></a>
  <a id="home" href=#><span>Home</span></a>
</div>
Run Code Online (Sandbox Code Playgroud)

但是,如果未指定id,如何应用addEventListener()?因为一些元素将具有相同的id.所以我将更改id属性

<div id="topmenu">
  <a mnuid="help" href=#><span>Help</span></a>
  <a mnuid="frum" href=#><span>Forum</span></a>
  <a mnuid="home" href=#><span>Home</span></a>
</div>
Run Code Online (Sandbox Code Playgroud)

html javascript addeventlistener

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

获取nil:此代码中的NilClass(NoMethodError)

我正在开始ruby书,这是我的代码,它给出了以下错误:

in 'show_current_description': undefined method 'full_description' for nil:NilClass (NoMethodError)

代码如下.任何帮助表示赞赏.谢谢!

class Dungeon
  attr_accessor :player

  def initialize(player_name,start_location)
    @player=Player.new(player_name,start_location)
    puts @player.location
    @rooms=[]
    show_current_description
  end

  def show_current_description
    @rm=find_room_in_dungeon(@player.location)
    @rm.full_description
  end

  def find_room_in_dungeon(reference)
    @rooms.detect{|room| room.reference == reference; puts room.full_description}
  end
  def add_room(reference,name,description,connections)
    @rooms << Room.new(reference,name,description,connections)
  end

  Player=Struct.new(:name,:location)

  class Room 
    attr_accessor :reference, :name, :description, :connections

    def initialize(reference,name,description,connections)
      @reference=reference
      @name=name
      @description=description
      @connections=connections
    end

    def full_description
      "You are in " + @description
    end
  end
end

d=Dungeon.new("Good Man",:small_cave)
d.add_room(:small_cave,"Small Cave","This is a small claustrophobic cave", {:east => :largecave}) …
Run Code Online (Sandbox Code Playgroud)

ruby

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

Ruby初学者课题

在我开始使用rails dev之前,我正在尝试更深入地学习ruby,但是我在学习课程时遇到了一些问题.我似乎无法理解为什么以下不起作用.

#point.rb
class Point
  attr_accessor :x, :y

  def initialize(p = [0,0])
   @x = p[0]
   @y = p[1]
  end
end

#shape.rb
require_relative 'point.rb'

class Shape

  attr_accessor :points

  def initialize *the_points
    for p in the_points
      @points.append Point.new(p)
    end
  end

end

s = Shape.new([3,2])

puts s.points
Run Code Online (Sandbox Code Playgroud)

当我调用该函数时,我得到NilClass的no方法错误,我假设它是指@ point.append.

ruby class

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

如何用':'打破%w'dog中的单词:cat:bird'w/o split

我试图这样做,%w'dog:cat:bird'但我希望将这些词分开的角色是:而不是像%w现在这样的空白.

我不想.split在实际代码中使用我使用一些不同的%习惯用于不同的需求,我想只使用一种语法.

ruby syntax

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

如何按特定值过滤模型的行?

我想只返回属于用户的元素.该user_id值是Run中的一列.我想要的只是选择runs来自的current_user.

def index
  @runs = Run.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @runs }
  end
end
Run Code Online (Sandbox Code Playgroud)

试过像

@runs = Run.where(user_id= => current_user.id)
Run Code Online (Sandbox Code Playgroud)

但它没有奏效.我该怎么办?

ruby activerecord ruby-on-rails

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

从字符串创建数组

我有一个网页,用户可以从jquery列表中选择多个项目.

根据选择的项目,我需要将每个项目添加到数据库中.

当有人选择一个项目时,返回到我的Javascript的值类似于"4~2"

值4将在我的示例中用于数据库中名为"skill_id"的一列,值2将用于同一行中名为"category_id"的另一列.

当有人选择两个项目时,它是逗号分隔的,类似于"4~2,6~7",如果选择超过2,则依此类推.

我想我需要使用数组或jquery.each()函数进行for循环,但不确定如何处理这个问题的最佳方法.

javascript arrays jquery

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

如何创建"全部检查"JavaScript方法

我很感兴趣如何创建一个调用JavaScript方法的按钮,它使得h:table(简单的html表)中的所有复选框都被检查?以及如何使用JavaScript方法再次取消选中表中的所有复选框?你能告诉我一些代码剪切或工作的例子吗?

javascript

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

使用canvas代替img有什么好处?

我目前正在构建一个移动应用程序,我只是想知道使用 canvas 或 img 标签是否更好(性能方面)。我将在我的网站上查询实际图像。只是想知道其中一种是否比另一种有好处。

mobile canvas image

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