尝试将哈希结果推送到数组 - Ruby on Rails

发布于 2024-12-08 19:10:15 字数 1219 浏览 0 评论 0原文

我刚刚开始(希望如此!)学习编程/ ruby​​ on Rails 并尝试使用以下方法将哈希结果推送到数组:

ApplicationController:

def css_class
  css = Array.new
  product =  {@product.oil => ' oil', @product.pressure_meters => ' pressure_meters', @product.commercial => 'commercial'}
  product.each do |key, value| 
    if key == true
       css.push(value)
    end
  end
  сss.join
end

And this in the ProductsController:

def create
  @product = Product.new(params[:product])
  @product.css_class = css_class

  respond_to do |format|
    if @product.save
      format.html { redirect_to @product, notice: 'Product was successfully created.' }
      format.json { render json: @product, status: :created, location: @product }
    else
      format.html { render action: "new" }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

这似乎只保存最后推送的内容对于数组,我自己尝试了下面的代码,它似乎有效,所以我很困惑我哪里出错了?

def css_class
  css = Array.new
  product =  {1 => ' pressure_meters', 2 => ' oil'}
  product.each do |key, value| 
    if key > 0
      css.push(value)
     end
  end
  css.join
end

puts css_class

提前致谢。

I'm just beginning to (hopefully!) learn programming / ruby on rails and trying to push the results of a hash to an array using:

ApplicationController:

def css_class
  css = Array.new
  product =  {@product.oil => ' oil', @product.pressure_meters => ' pressure_meters', @product.commercial => 'commercial'}
  product.each do |key, value| 
    if key == true
       css.push(value)
    end
  end
  сss.join
end

And this in the ProductsController:

def create
  @product = Product.new(params[:product])
  @product.css_class = css_class

  respond_to do |format|
    if @product.save
      format.html { redirect_to @product, notice: 'Product was successfully created.' }
      format.json { render json: @product, status: :created, location: @product }
    else
      format.html { render action: "new" }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

This only seems to only save the last thing that was pushed to the array, I tried the below code on it's own and it seems to work, so I'm baffled as to where I'm going wrong?

def css_class
  css = Array.new
  product =  {1 => ' pressure_meters', 2 => ' oil'}
  product.each do |key, value| 
    if key > 0
      css.push(value)
     end
  end
  css.join
end

puts css_class

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-12-15 19:10:15

在 Ruby 中,Hash 不能有重复的键,因此

def css_class
  css = Array.new
  product = { @product.oil => ' oil', 
              @product.pressure_meters => ' pressure_meters', 
              @product.commercial => 'commercial' }
  product.each do |key, value| 
    if key == true
      css.push(value)
    end
  end
  сss.join
end

不起作用,因为

irb(main):0> h = { true => 'foo', true => 'bar', false=>'foo', false => 'bar' }
=> {true=>"bar", false=>"bar"}

您的第二个示例仅因为您有不同的键 (1,2) 才有效,所以让我们稍微重构一下您的代码

def css_class
  css = ""
  product = { ' oil' => @product.oil, 
              ' pressure_meters' => @product.pressure_meters, 
              ' commercial' => @product.commercial }
  product.each do |key, value| 
    css << key  if value
  end

  сss.strip
end

它可以进一步简化,但是以前的版本也应该可以正常工作

def css_class
  [ "oil ", "pressure_meters ", "commercial " ].inject(""){ |sum, val| sum += val if @product.send( val.strip ) }.strip
end

In Ruby Hash can't have duplicate keys so

def css_class
  css = Array.new
  product = { @product.oil => ' oil', 
              @product.pressure_meters => ' pressure_meters', 
              @product.commercial => 'commercial' }
  product.each do |key, value| 
    if key == true
      css.push(value)
    end
  end
  сss.join
end

will not work because

irb(main):0> h = { true => 'foo', true => 'bar', false=>'foo', false => 'bar' }
=> {true=>"bar", false=>"bar"}

your second example works only because you have distinct keys (1,2) so let's refactor your code a bit

def css_class
  css = ""
  product = { ' oil' => @product.oil, 
              ' pressure_meters' => @product.pressure_meters, 
              ' commercial' => @product.commercial }
  product.each do |key, value| 
    css << key  if value
  end

  сss.strip
end

it can be simplified even more however previous version should work fine too

def css_class
  [ "oil ", "pressure_meters ", "commercial " ].inject(""){ |sum, val| sum += val if @product.send( val.strip ) }.strip
end
奈何桥上唱咆哮 2024-12-15 19:10:15

您可以使用 Hash#values 获取哈希值的数组。

所以:

product_values = product.values

有条件的话,你可以使用 select 选择你想要的,如下所示:

product_values = product.select {|k,v| k == true }.values

这是详细的:

product_values = product.select {|k,v| k }.values

You can use Hash#values to get an array of your hash's values.

So:

product_values = product.values

And conditionally, you could pick the ones you want using select, like this:

product_values = product.select {|k,v| k == true }.values

Which is verbose for:

product_values = product.select {|k,v| k }.values
脱离于你 2024-12-15 19:10:15

感谢您为我指明了正确的方向。我的代码 Bohdan 一直收到 500 内部服务器错误,不知道为什么,但尝试了一下并最终发现它可以工作:

def css_class
css = Array.new
   product = { ' oil' => @product.oil, 
            ' pressure_meters' => @product.pressure_meters, 
            ' commercial' => @product.commercial }
   product.each do |key, value| 
  css << key  if value 
end

  css.join
end

Thanks for pointing me in the right direction. I kept getting a 500 internal server error with your code Bohdan, not sure why, but played around with it and eventually found this to work:

def css_class
css = Array.new
   product = { ' oil' => @product.oil, 
            ' pressure_meters' => @product.pressure_meters, 
            ' commercial' => @product.commercial }
   product.each do |key, value| 
  css << key  if value 
end

  css.join
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文