![](/img/trans.png)
[英]How to create an array of hashes by mapping 2 hashes in ruby on rails
[英]how to create a array of hashes in Rails 4?
我正在尝试通过我的自定义烤面包机通知进行此操作
这就是我所拥有的
def toast(type, text)
flash[:toastr] = { type => text }
end
我在控制器中这样称呼它
toast('success',"this is a message")
它会像这样输出到我的模板
<% flash[:toastr].each do |type, message| %>
toastr.<%= type %>('<%= message %>')
<% end %>
但是它只输出1条消息
现在,这里是我要尝试的功能,它显示多个Flash消息http://tomdallimore.com/blog/extending-flash-message-functionality-in-rails/
它的工作原理是由于以下方法
def flash_message type, text
flash[type] ||= []
flash[type] << text
end
每次调用#flash_message
,都会将Flash消息保存在一个数组中,我可以使用该数组中的for each
消息来显示它。
我在将#toast
转换为该#toast
时遇到问题, #toast
目前正在执行此操作
flash[:toastr] = {'success' => "this is a message"}
我想这样做
flash[:toastr] = [{'success' => "this is a message'},{'error' => "problem!"}]
有人可以帮我修改toast
方法以接受哈希数组,并在每次调用时插入新的哈希吗?
def toast(type, text)
flash[:toastr] ||= []
flash[:toastr] << { type => text }
end
使用Array#push
:
def toast type, text
flash[:toastr] ||= []
flash[:toastr].push({ type => text })
end
使用append( <<
):
def toast type, text
flash[:toastr] ||= []
flash[:toastr] << { type => text }
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.