Rails:未定义的方法`title'为零:NilClass

时间:2015-11-26 18:29:35

标签: ruby-on-rails

所以我搜索了这个并且大多数人都遇到了这个错误,因为他们在他们的Class中间有他们的私有方法,或者他们在db中有nil属性的记录。就目前而言,我在数据库中只有一条记录,我将在下面显示。尝试访问我正在创建的索引页时出现此错误。

控制器:

class AnthologiesController < ApplicationController
    before_action :authenticate_user!, :except => [:show, :index]

    def index
        @anthologies = Anthology.all
    end

    def show
        @anthology = Anthology.find(params[:id])
    end

    def new
        @anthology = Anthology.new
    end

    def create
        @anthology = Anthology.new(anthology_params)
        @anthology.user = current_user

        if @anthology.save
            redirect_to @anthology, notice: "Success! Your anthology was created."
        else
            render 'new'
        end
    end

    private
    def anthology_params
        params.require(:anthology).permit(:title, :description)
    end
end

查看:

<h1>Anthologies</h1>
<ul>
    <%= @anthologies.each do |anthology| %>
        <li><%= @anthology.title %></li>
        <li><%= @anthology.description %></li>
        <li><%= @anthology.username %></li>
    <% end %>
</ul>

db中的一条记录:

#<Anthology id: 1, title: "Writing More Better", description: "This is me teaching you writing and suchwhat.", created_at: "2015-11-26 15:57:40", updated_at: "2015-11-26 15:57:40", user_id: 1>

任何帮助都会令人惊讶,并提前感谢。

2 个答案:

答案 0 :(得分:3)

each循环中,您指的是@anthology,而不是您声明的块变量。

<h1>Anthologies</h1>
<ul>
    <%= @anthologies.each do |anthology| %>
        <li><%= anthology.title %></li>
        <li><%= anthology.description %></li>
        <li><%= anthology.username %></li>
    <% end %>
</ul>

答案 1 :(得分:0)

即使你也可以试试这个:

kill `ps -ef | grep 'java Client' | grep -v grep | awk '{print $3}'`
相关问题