Rails 3 tag_list.each
我在尝试从 acts_as_taggable_on tag_list 制作列表时遇到问题 我有标签列表数组,我想列出它,所以我尝试这样做:
<%= proyects.tag_list.each do |tagsx| %>
* <%= tagsx %> <br>
<% end %>
我得到了我正在寻找的列表,但也得到了整个数组...... 当它渲染时,看起来像这样..
* AJAX
* Rails
* Heroku
* Prototype
AJAX, Rails, Heroku, Prototype
关于删除最后一行有什么想法吗? 或者你们知道实现这一目标的更有效方法吗?
提前致谢。
I have a problem trying to make a list from a acts_as_taggable_on tag_list
I have tag list array, and I want to list it so im trying this:
<%= proyects.tag_list.each do |tagsx| %>
* <%= tagsx %> <br>
<% end %>
And I get the list im looking for, but also the whole array again...
When it renders, looks like this..
* AJAX
* Rails
* Heroku
* Prototype
AJAX, Rails, Heroku, Prototype
Any ideas on getting rid of the last line?
Or do you guys know a more efficient way of achieving this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改
为:
您不想输出
.each
调用的返回值,而只想输出数组的元素。调用Array#each
块返回数组(就像你一样):这就是逗号分隔列表的来源。
Change this:
to this:
You don't want to output the return value of the
.each
call, just the elements of the array. CallingArray#each
with a block returns the array (as you are):and that's were the comma delimited list is coming from.
因为你的代码中有一个拼写错误:-)
看到区别了吗?
第一个 % 符号后没有“=”
%= 表示 Ruby 表达式的结果返回到视图
%- 表示计算 Ruby 表达式,但不返回结果
您问题中的代码获取“proyects.tag_list” ,执行循环,在此期间打印出各个标签,然后由于“=”而将整个数组返回到视图
because you have a typo in your code :-)
see the difference?
no '=' after the first % sign
%= means that the result of a Ruby expression is returned to the view
%- means that the Ruby expression is evaluated, but no result is returned
The code in your question gets "proyects.tag_list" , executes the loop, during which it prints out the individual tags, and then returns the whole array to the view because of the '='