小编bla*_*ula的帖子

按键分组并对值求和

我有一系列哈希:

[{"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万
查看次数

Rails有很多通过多态复选框

这个真的让我失望!:(

我正在尝试为我的用户模型创建一个嵌套模型表单,其中包含一个复选框列表,其中可以检查或取消选中多个商店以通过模型人员配置来管理商店.

class Staffing < ActiveRecord::Base
  # Associations
  belongs_to :user
  belongs_to :staffable, :polymorphic => true
  belongs_to :store,     :class_name => "Store",
                         :foreign_key => "staffable_id"
end

class User < ActiveRecord::Base
  # Includes
  acts_as_authentic

  # Associations
  has_many :staffings, :dependent => :destroy
  has_many :stores, :through => :staffings

  # Nested Attributes
  accepts_nested_attributes_for :staffings, :allow_destroy => true
end

class Store < ActiveRecord::Base
  # Associations
  has_many :staffings, :as => :staffable, :dependent => :destroy
  has_many :users, :through => :staffings
end


# Users Controller
def create
  @user = User.new(params[:user]) …
Run Code Online (Sandbox Code Playgroud)

forms checkbox ruby-on-rails has-many-polymorphs has-many-through

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

相对于Fancybox增加身高

我使用Fancybox在Intranet站点上显示复杂的表单.Fancybox表单的页面是使用Datatables来过滤过滤器并对数据库中的大量记录进行排序.当列表被显着过滤并加载Fancybox时,Fancybox内容可能会超出页面正文.这会导致Fancybox叠加层未显示的白色区域.

除了丑陋之外,您无法单击此区域以关闭Fancybox.

使用CSS,Javascript或两者,将页面主体增长到至少Fancybox的高度的最佳方法是什么?

谢谢!

编辑:下面的Gov解决方案是正确的,但没有解决问题.实际上,需要增加Fancybox覆盖层本身的高度.我现在认为这是Fancybox的一个错误.看起来如果身体因Fancybox尺寸而增长,那么叠加层应该自动增加到身体尺寸.我会将此提交给开发人员.与此同时,如果有人有这个问题Gov的代码,因为我已修改它将工作:

// Adjust Fancybox Overlay Height to Large Fancyboxes
function overlayHeight() {
  fancyBoxHeight = $('#fancybox-wrap').height(); 
  finalheight = fancyBoxHeight + 300;
  $('#fancybox-overlay').css('height', finalheight);
}
Run Code Online (Sandbox Code Playgroud)

我还想注意Fancybox是一个非常好的插件,所以不应该让任何人反对使用它!

css jquery fancybox

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