【发布时间】: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方法? -
#<Note id: nil, created_at: nil, updated_at: nil>好像没有内容字段,你的t.text :content是在你运行db:migrate命令后添加的吗?
标签: html ruby-on-rails ruby