【问题标题】:undefined method `content' for #<Note id: nil, created_at: nil, updated_at: nil>#<Note id: nil, created_at: nil, updated_at: nil> 的未定义方法“内容”
【发布时间】:2015-08-09 09:50:21
【问题描述】:

我之前似乎已经发布过这个问题,但正常的“将:内容添加到模型”不适用于我的情况。我已经添加了它,错误仍然存​​在。

如果看起来很熟悉,这是一个 codecademy 项目的修改版本。

型号

 class CreateNotes < ActiveRecord::Migration
    def change
      create_table :notes do |t|
        t.text :content
        t.timestamps
      end
    end
 end

控制器

class NotesController < ApplicationController
    def index
       @notes = Note.all
    end

    def new
       @note = Note.new
    end

    def create
       @note = Note.new(note_params)
           if @note.save
               redirect_to '/notes'
           else
               render 'new'
           end
    end

    private
    def note_params
       params.require(:note).permit(:content)
    end
end

路线

Rails.application.routes.draw do

     root 'notes#index'
     get "notes" => "notes#index"
     get "notes/new" => "notes#new"
     post "notes" => "notes#create"

index.html.erb

<div class="header">
  <div class="container">
    <h1>Notes</h1>
  </div>
</div>

<div class="notes">
  <div class="container">

    <% @notes.each do |note| %>
        <div class="note">
        <p class="content"><%= note.content %></p>
        <p class="time"><%= note.created_at %></p>
        </div>
    <% end %>
    <%= link_to 'New Note', "notes/new" %>

  </div>
</div>

new.html.erb

<div class="header">
  <div class="container">
    <h1>Notes</h1>
  </div>
</div>

<div class="create">
  <div class="container">

    <%= form_for(@note) do |f| %>
        <div class="field">
            <%= f.label :note %><br>
            <%= f.text_area :content %>
        </div>
        <div class="actions">
            <%= f.submit "Create" %>
        </div>
        <% end %>
  </div>
</div>

如果有人能弄清楚为什么我在 :content 已经在模型中之后仍然收到此错误,那就太棒了!

附:第一次发帖很抱歉,如果很糟糕。

【问题讨论】:

  • 执行bundle exec rake db:migrate 以运行迁移。重新启动服务器并重试。
  • 我试了很多次都没用。
  • 运行迁移后(注意环境),如果运行rails c,是否可以创建Note,并使用content方法?
  • #&lt;Note id: nil, created_at: nil, updated_at: nil&gt; 好像没有内容字段,你的t.text :content 是在你运行db:migrate 命令后添加的吗?

标签: html ruby-on-rails ruby


【解决方案1】:

ActiveRecord 通过检查数据库中的列在模型上创建方法content。由于方法未定义,我想你可能忘记运行迁移了。

该列不存在的可能原因:

  • 在运行迁移和运行应用程序时,您没有连接到同一个数据库。例如,您可能在不同的环境中运行。
  • 您的迁移实际上并未添加所需的列。检查您的迁移。

可能的检查方式:

  • “手动”检查数据库以确认该列确实存在。

【讨论】:

  • 我已经多次运行迁移,但仍然出现错误。
  • 如果你启动一个 Rails 控制台并写例如Note.new.content,你会得到 no method 错误吗?
  • 是的,但我不确定我输入的命令是否正确。我用 .bat 文件在 Windows 上设置了 ruby​​。我对 ruby​​ 和 rails 也很陌生。我尝试输入“Note.new.content”、“rails Note.new.content”和“ruby Note.new.content”。他们都提出了错误,说命令不存在或文件不存在。感谢您的帮助!
  • 我不知道如何在 Windows 上设置 ruby​​ 或 rails,但如果你有一个 rails 可执行文件,你应该可以编写 rails console 来启动控制台。在那个控制台你可以写Note.new.content。如果这也引发了NoMethod 错误,那么您就知道您的数据库没有被正确迁移。
  • 是的,NoMethod 错误。我从这里去哪里?我已经使用 'rake db:migrate' 执行了多次迁移。再次感谢您的帮助!
猜你喜欢
  • 2013-07-01
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 2015-05-28
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多