新手问题 学习多态遇到的一点问题

buxiangshuo · April 11, 2013 · Last by buxiangshuo replied at April 11, 2013 · 2144 hits

刚刚开始学习 ruby on rails 正在做comment的时候学习多态的用法 按照这个做的:http://terenceponce.github.io/blog/2012/03/02/polymorphic-associations-in-rails-32/ 我 scaffold 了docissuediscuss,想要每个都有comment 但是不知道为什么运行后抛出这个错误:undefined method 'each' for nil:NilClass 请问怎样解决? 谢谢大家~

这问题跟多态无关,根据链接

<% @comments.each do |comment| %>

这个 @comments 可能没有赋值

#1 楼 @Rei 谢谢你~~我又仔细看了一下

可以在comments单独的view里面操作

类似http://127.0.0.1:3000/docs/1/comments/new,然后给doc添加评论

但是无法在docsshow页面显示评论,这时@comment就出错了~

controller 裏面 show 方法是怎麼取 @comments 的?

问题解决了,是我太疏忽

忘记调用@doc.comments.each

不过还是谢谢大家

You need to Sign in before reply, if you don't have an account, please Sign up first.