Ruby on rails “ActiveRecord::语句在ArticleShow中无效”jumpstart教程
我正在JumpstartAB.com上编写博客教程。我对此有奇怪的问题。当我运行服务器时,它看起来非常好。我看到了/articles下的文章列表。但当我试图看到其中一个时,我得到了一个错误:Ruby on rails “ActiveRecord::语句在ArticleShow中无效”jumpstart教程,ruby-on-rails,Ruby On Rails,我正在JumpstartAB.com上编写博客教程。我对此有奇怪的问题。当我运行服务器时,它看起来非常好。我看到了/articles下的文章列表。但当我试图看到其中一个时,我得到了一个错误: ActiveRecord::StatementInvalid in Articles#show Showing /home/rails_projects/blogger/app/views/articles/show.html.erb where line #6 raised: SQLite3::SQL
ActiveRecord::StatementInvalid in Articles#show
Showing /home/rails_projects/blogger/app/views/articles/show.html.erb where line
#6 raised:
SQLite3::SQLException: no such column: comments.article_id: SELECT COUNT(*) FROM
"comments" WHERE "comments"."article_id" = ?
Extracted source (around line #6):
3
4 <%= link_to "edit", edit_article_path(@article) %>
5
6 <h3>Comments(<%= @article.comments.count %>)</h3>
7
8 <%= render partial: 'articles/comment', collection: @article.comments %>
9 <%= render partial: 'comments/form' %>
Show.html.erb:
<%= link_to "edit", edit_article_path(@article) %>
<h3>Comments(<%= @article.comments.count %>)</h3>
<%= render partial: 'articles/comment', collection: @article.comments %>
<%= render partial: 'comments/form' %>
<%= link_to "<< Back to Articles List", articles_path%>
<%= link_to "edit", edit_article_path(@article) %>
<%= link_to "delete", article_path(@article),method: :delete, :confirm => "Really
delete the article?"
文章\u controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(params[:article])
@article.save
redirect_to article_path(@article)
end
def article_params
params.require(:article).permit(:title, :body)
end
def update
@article = Article.find(params[:id])
@article.update_attributes(params[:article])
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
end
def change
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
def new
@article = Article.new
end
end
20130716025552_create_articles.rb
Class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.integer :article_id
t.string :title
t.text :body
t.timestamps
end
end
end
20130717021354_create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :author_name
t.integer :article_id
t.text :body
t.string :article
t.string :references
t.timestamps
end
end
end
我很困惑,真的不知道该怎么处理这个问题。首先,运行bundle exec rake db:rollback,它将回滚您的上一次迁移,这很好,因为“create comments”迁移是最后一次迁移。然后,编辑迁移文件:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :author_name
t.text :body
t.references :article
t.timestamps
end
end
end
然后再次运行bundle exec rake db:migrate。是否运行了迁移?似乎缺少迁移,项目id不存在。注释表中是否有项目id列或其他类似内容?是的,我将粘贴迁移文件。