0

jumpstartlab.com の Blogger チュートリアルに取り組んでいます。私はそれに奇妙な問題を抱えています。サーバーを実行すると、かなり良さそうです。「/articles」の下に記事のリストが表示されます。しかし、それらの1つを見ようとすると、エラーが発生します:

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

私は非常に混乱しており、この問題をどうすればよいか本当にわかりません。

4

1 に答える 1

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します。

于 2013-07-24T11:37:48.087 に答える