模块数组输出
我正在编写一个模块来向 FlagShihTzu gem 添加功能。
基本上它会遍历标志并输出分配给对象的键。它正在工作,但我还希望能够使用视图中的块来处理输出。
问题是它同时输出模块中的数组和视图中块的输出。
module AwesomeFlags
def my_flags(column = nil)
a = self.flag_mapping
if column.nil?
c = a.values.map {|var| var.keys}.flatten
else
b = a[column]
c = Array.[](b.keys).flatten
end
c.map {|var| self.send(var) ? "#{var.to_s} " : nil}.compact!
end
end
在视图中:
= book_offer.my_flags.each do |flag|
= flag.titleize
我得到的是:
Regular Complimentary regular complimentary
I'm writing a module to add functionality to the FlagShihTzu gem.
Basically it goes through the flags and outputs the keys for the ones assigned to the object. It's working, but I also want to be able to use a block in the view to do things with the output.
The problem is that it's outputting both the array from the module and the output from the block in the view.
module AwesomeFlags
def my_flags(column = nil)
a = self.flag_mapping
if column.nil?
c = a.values.map {|var| var.keys}.flatten
else
b = a[column]
c = Array.[](b.keys).flatten
end
c.map {|var| self.send(var) ? "#{var.to_s} " : nil}.compact!
end
end
In the view:
= book_offer.my_flags.each do |flag|
= flag.titleize
What I get is:
Regular Complimentary regular complimentary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将其更改为:
=
表示包含方法调用的输出,其中-
表示简单地执行它。each
循环返回列表中的项目。You should switch that to be:
The
=
means to include the output of the method call, where-
means to simply execute it. Theeach
loop is returning the items in the list.