开发者

Strange Datamapper's behaviour

I have this code in my controller:

 @cats = DirCat.all

And this in a view:

  %ul#menu
    = @cats.each do |item|
      %li
        = link_to item.title, "/catalog/#{item.id}/"

And get strange output:

    <ul id='menu'>
      <li>
        <a href="/catalog/4/">hello</a>
      </li>
      <li&g开发者_StackOverflow中文版t;
        <a href="/catalog/5/">hello 1</a>
      </li>
    #<DataMapper::Collection:0x85a9d00>
    </ul>

In the irb console:

irb(main):002:0> c.each { |item| puts item.title }
hello
hello 1
=> [#<DirCat @id=4 @parent_id=1 @title="hello">, #<DirCat @id=5 @parent_id=1 @title="hello 1">]

How can I get out #<DataMapper::Collection:0x85a9d00> from my html?


In HAML, = is used to display the output of a thing in your markup. #<DataMapper::Collection:0x85a9d00> is the return value of @cats.each do |item|. You want to use -, which executes code (but does not print the return value):

- @cats.each do |item|
   %li
     = link_to item.title, "/catalog/#{item.id}/"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜