标签: ruby

在rails上的ruby中创建自定义html助手

我在一年前开始编写ASP.NET MVC Framework.最近.我学习了Ruby On Rails框架ASP.NET MVC中有"自定义html助手"功能所以我可以创建自己的html帮助器

<%= Html.MyOwnHtmlHelper() %>
Run Code Online (Sandbox Code Playgroud)

我已经了解到Ruby中有html助手,例如

<% text_area %>
Run Code Online (Sandbox Code Playgroud)

在html呈现

我有个问题.我可以创建自己的html帮助器来渲染我自己的html吗?

ruby ruby-on-rails

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

Sum 2使用相同的键散列属性

我有2个哈希,例如:

{'a' => 30, 'b' => 14}
{'a' => 4, 'b' => 23, 'c' => 7}
Run Code Online (Sandbox Code Playgroud)

其中a,bc都是对象.我怎样才能将这些哈希的密钥加起来得到一个新的哈希:

{'a' => 34, 'b' => 37, 'c' => 7}
Run Code Online (Sandbox Code Playgroud)

ruby hash attributes sum

40
推荐指数
2
解决办法
9356
查看次数

ruby是否支持范围语句中的范围?

我想做这个:

case cost

    when cost between 1 and 3 then cost * 1.1
    when cost between 3 and 5 then cost * 1.2
else
    0
Run Code Online (Sandbox Code Playgroud)

ruby

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

如何将消息附加到rspec检查?

在rspec中:我可以像在xUnit样式测试框架中那样将消息附加到支票上吗?怎么样?

assert_equal value1, value2, "something is wrong"
Run Code Online (Sandbox Code Playgroud)

ruby tdd rspec xunit

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

按键分组并对值求和

我有一系列哈希:

[{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}]
Run Code Online (Sandbox Code Playgroud)

我需要在inject这里使用,但我确实在苦苦挣扎.

我想要一个反映前一个哈希重复键总和的新哈希:

[{"Vegetable"=>15}, {"Dry Goods"=>5}]
Run Code Online (Sandbox Code Playgroud)

我控制输出此哈希的代码,以便我可以在必要时进行修改.结果主要是哈希,因为这可能会最终嵌套任意数量的级别,然后很容易在数组上调用flatten但不会平滑哈希的键/值:

def recipe_pl(parent_percentage=nil)
  ingredients.collect do |i|

    recipe_total = i.recipe.recipeable.total_cost 
    recipe_percentage = i.ingredient_cost / recipe_total

    if i.ingredientable.is_a?(Purchaseitem)
      if parent_percentage.nil?
        {i.ingredientable.plclass => recipe_percentage}
      else
        sub_percentage = recipe_percentage * parent_percentage
        {i.ingredientable.plclass => sub_percentage}
      end
    else
      i.ingredientable.recipe_pl(recipe_percentage)
    end
  end
end 
Run Code Online (Sandbox Code Playgroud)

ruby hash key enumerable

40
推荐指数
3
解决办法
3万
查看次数

嵌套模型和父级验证

我有两个型号.
- Parent has_many Children ;
- Parent accepts_nested_attributes_for Children ;

class Parent < ActiveRecord::Base
  has_many :children, :dependent => :destroy
  accepts_nested_attributes_for :children, :allow_destroy => true
  validates :children, :presence => true
end

class Child < ActiveRecord::Base
  belongs_to :parent
end
Run Code Online (Sandbox Code Playgroud)

我使用验证来验证每个父母的孩子的存在,所以我不能保存没有孩子的父母.

parent = Parent.new :name => "Jose"
parent.save
#=> false
parent.children_attributes = [{:name => "Pedro"}, {:name => "Emmy"}]
parent.save
#=> true
Run Code Online (Sandbox Code Playgroud)

验证工作.然后我们将通过_destroy属性摧毁孩子:

parent.children_attributes = {"0" => {:id => 0, :_destroy => true}}
parent.save
#=> true !!!
parent.reload.children
#=> []
Run Code Online (Sandbox Code Playgroud)

所以我可以通过嵌套表单销毁所有孩子,验证将通过. …

ruby ruby-on-rails nested-attributes

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

在`respond_to'格式语句中,`:location => ...`和`head:ok`是什么意思?

我正在使用Ruby on Rails 3,我想知道以下代码中的:location => ...head :ok语句含义,它们如何工作以及如何使用它们.

respond_to do |format|
    format.xml  { render :xml => @user, :status => :created, :location => @user }
end

respond_to do |format|
    format.xml  { head :ok }
end
Run Code Online (Sandbox Code Playgroud)

ruby format ruby-on-rails respond-to ruby-on-rails-3

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

如何在Rails 3中订购包含的元素

我有一个模型关系,其中today有很多tasks

我正在尝试检索用户的today对象,包括tasks并将它们全部呈现给Json.所有这一切都很顺利,直到我决定要taskstoday对象内进行排序,因为respond_with block它也用于渲染html页面.有没有办法包括tasks和订购它们?

我正在尝试这样的事情:

class TodaysController < ApplicationController
  respond_to :html, :json
  def show
    @today = Today.where(:user_id => current_user.id).joins(:tasks).includes(:tasks).order(:priority).first
    respond_with @today, :include => :tasks
  end
end
Run Code Online (Sandbox Code Playgroud)

这会正确检索所有内容,但似乎根本不会对任务进行排序.

这就是我曾经拥有的(效果很好,但没有订购):

class TodaysController < ApplicationController
  respond_to :html, :json
  def show
    @today = current_user.today
    respond_with @today, :include => :tasks
  end
end
Run Code Online (Sandbox Code Playgroud)

我知道我可以检索数据并在之后对其进行排序,如下所示:

@today = current_user.today
@today.tasks.sort!{|a,b| a.priority <=> b.priority }
Run Code Online (Sandbox Code Playgroud)

这有效并将通过我的测试,但我希望有一种ActiveRecord方法来解决这个问题.

ruby activerecord ruby-on-rails arel

40
推荐指数
2
解决办法
2万
查看次数

Rails 3.1中的Rails.cache错误 - TypeError:无法使用默认proc转储哈希

我在3.1.0.rc4上遇到了Rails.cache方法的问题(ruby 1.9.2p180(2011-02-18修订版30909)[x86_64-darwin10]).该代码在2.3.12(ruby 1.8.7(2011-02-18 patchlevel 334)[i686-linux],MBARI 0x8770,Ruby Enterprise Edition 2011.03)上的相同应用程序中正常工作,但在升级后开始返回错误.我还没弄清楚原因.

尝试缓存具有多个范围的对象时,似乎会发生错误.

此外,无论使用多少范围,使用lambdas的任何范围都会失败.

我从这些模式中遇到了失败:

Rails.cache.fetch("keyname", :expires_in => 1.minute) do
    Model.scope_with_lambda
end


Rails.cache.fetch("keyname", :expires_in => 1.minute) do
    Model.scope.scope
end
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误:

TypeError: can't dump hash with default proc
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:627:in `dump'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:627:in `should_compress?'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:559:in `initialize'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:363:in `new'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:363:in `block in write'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:520:in `instrument'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:362:in `write'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/activesupport-3.1.0.rc4/lib/active_support/cache.rb:299:in `fetch'
    from (irb):62
    from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
    from /project/shared/bundled_gems/ruby/1.9.1/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用:raw => …

ruby memcached caching ruby-on-rails ruby-on-rails-3

40
推荐指数
2
解决办法
2万
查看次数

在Ruby中创建数字,字符串,数组或散列的md5哈希

我需要在Ruby中为变量创建一个签名字符串,其中变量可以是数字,字符串,散列或数组.散列值和数组元素也可以是这些类型中的任何一种.

此字符串将用于比较数据库中的值(在本例中为Mongo).

我的第一个想法是创建一个JSON编码值的MD5哈希,如下所示:( body是上面提到的变量)

def createsig(body)    
  Digest::MD5.hexdigest(JSON.generate(body))
end
Run Code Online (Sandbox Code Playgroud)

这几乎可以工作,但JSON.generate不会每次以相同的顺序对哈希的键进行编码,因此createsig({:a=>'a',:b=>'b'})并不总是相等createsig({:b=>'b',:a=>'a'}).

创建符合此需求的签名字符串的最佳方法是什么?

注意:对于我们之间的细节,我知道你不能JSON.generate()是数字或字符串.在这些情况下,我只是MD5.hexdigest()直接打电话.

ruby md5 digest

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