【问题标题】:undefined method `author' for nil:NilClassnil:NilClass 的未定义方法“作者”
【发布时间】:2014-07-22 11:33:32
【问题描述】:

我的 rails 应用程序有一些问题。

<% @story.chapters.each do |chapter| %>
    <% if round.to_s != chapter.round %> 
        <% if chosenChapterRound != chapter.round %>
            <% maxVotes = 0 %>
        <% end %>
        <% if chapter.votes.count > maxVotes %>
            <% maxVotes = chapter.votes.count %>
            <% chosenChapterRound = chapter.round %>
            <% chosenChapters[round.to_i] = chapter %>
        <% end %>
    <% end %>
<% end %>
<% chosenChapters.each do |chosenChapter| %>
    <%= chosenChapter %>
<% end %>

这会输出如下内容:#

我真正想做的是:

<% chosenChapters.each do |chosenChapter| %>
    <%= chosenChapter.author %>
<% end %>

但每当我尝试访问 selectedChapter 中的某个属性时,我都会收到以下错误消息:

undefined method `author' for nil:NilClass

我做错了什么?谢谢!

【问题讨论】:

    标签: ruby-on-rails erb


    【解决方案1】:

    您的chosenChapters 中至少有一个是nil。问题可能出在以下片段中:

    <% maxVotes = chapter.votes.count %>
    <% chosenChapterRound = chapter.round %>
    <% chosenChapters[round.to_i] = chapter %>
    

    【讨论】:

    • 谢谢!果然如你所说。
    猜你喜欢
    • 2011-07-27
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多