尝试将哈希结果推送到数组 - Ruby on Rails
我刚刚开始(希望如此!)学习编程/ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Ruby 中,
Hash
不能有重复的键,因此不起作用,因为
您的第二个示例仅因为您有不同的键
(1,2)
才有效,所以让我们稍微重构一下您的代码它可以进一步简化,但是以前的版本也应该可以正常工作
In Ruby
Hash
can't have duplicate keys sowill not work because
your second example works only because you have distinct keys
(1,2)
so let's refactor your code a bitit can be simplified even more however previous version should work fine too
您可以使用
Hash#values
获取哈希值的数组。所以:
有条件的话,你可以使用
select
选择你想要的,如下所示:这是详细的:
You can use
Hash#values
to get an array of your hash's values.So:
And conditionally, you could pick the ones you want using
select
, like this:Which is verbose for:
感谢您为我指明了正确的方向。我的代码 Bohdan 一直收到 500 内部服务器错误,不知道为什么,但尝试了一下并最终发现它可以工作:
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: