“ActiveRecord :: StatementInvalid in Articles #show”jumpstart tutorial

时间:2013-07-24 11:20:10

标签: ruby-on-rails

我正在使用来自jumpstartlab.com的Blogger教程。我有奇怪的问题。当我运行服务器时它看起来很不错。我在“/ articles”下看到了一个文章列表。但是当我试图看到其中一个时,我得到一个错误:

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?" 

Articles_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

我很困惑,真的不知道该怎么处理这个问题。

1 个答案:

答案 0 :(得分:1)

首先,运行bundle exec rake db:rollback将回滚上次迁移(这很好,因为'创建评论'迁移是最后一次迁移)。然后,编辑您的迁移文件:

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