0

我正在与用户一起开发一个应用程序,他们每个人的页面上都显示了一组微博。我正在尝试为这些微博添加评论。每次访问 localhost:3000/users/(user_id_#)。标题中给出的错误中的 user_id 为 2。

SQLite3::SQLException:没有这样的列:comments.micropost_id: SELECT "comments".* FROM "comments" WHERE "comments"."micropost_id" = 2

仅当用户要显示微博时才会出现此错误。否则它只会显示他们的空白页。错误来自此视图 app/views/users/show.html.erb 此视图呈现此部分,错误发生在第 13 行。

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>
  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method:  :delete,
                                     confirm: "You sure?",
                                     title:   micropost.content %>
  <% end %>

  <h2>Comments</h2>
  <% micropost.comments.each do |comment| %>
    <p>
      <b>Commenter:</b>
      <%= comment.commenter %>
    </p>

    <p>
      <b>Comment:</b>
      <%= comment.body %>
    </p>
  <% end %>

  <h3>Add a comment:</h3>
  <%= form_for([micropost, micropost.comments.build]) do |f| %>
    <div class="field">
      <%= f.label :commenter %><br />
      <%= f.text_field :commenter %>
    </div>
    <div class="field">
      <%= f.label :body %><br />
      <%= f.text_area :body %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>
</li>

这是我的comment.rb 文件

class Comment < ActiveRecord::Base
  belongs_to :micropost
  attr_accessible :body, :user_id

end

和我的 micropost.rb 文件

class Micropost < ActiveRecord::Base
  attr_accessible :content
  belongs_to :user
  has_many :comments

  validates :content, presence: true, length: { maximum: 140 }
  validates :user_id, presence: true
end

和我的comments_controller.rb

class CommentsController < ApplicationController
  def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = @micropost.comments.create(params[:comment])
    redirect_to micropost_path(@micropost)
  end

end

最后是我的 microposts_controller.rb

class MicropostsController < ApplicationController
  before_filter :signed_in_user
  before_filter :correct_user,   only: :destroy

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end


  def new
    @micropost = Micropost.new(params[:micropost])
  end

  def show

    @micropost = Micropost.find(params[:id])


  end

  def destroy
    @micropost.destroy
    redirect_back_or root_path
  end

  private

    def correct_user
      @micropost = current_user.microposts.find_by_id(params[:id])
      redirect_to root_path if @micropost.nil?
    end
end


    class CommentsController < ApplicationController
      def create
        @micropost = Micropost.find(params[:micropost_id])
        @comment = @micropost.comments.create(params[:comment])
        redirect_to micropost_path(@micropost)
      end

    end

这里也是 users_controller.rb

class UsersController < ApplicationController
  before_filter :signed_in_user, 
                only: [:index, :edit, :update, :destroy, :following, :followers]
  before_filter :correct_user,   only: [:edit, :update]
  before_filter :admin_user,     only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])

  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User destroyed."
    redirect_to users_path
  end

  def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.followed_users.paginate(page: params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end

  private

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

    def admin_user
      redirect_to(root_path) unless current_user.admin?
    end
end

从错误看来,@micropost 没有在 def show 下的 microposts_controller.rb 文件中初始化。但我认为是?我究竟做错了什么?谢谢

这里也是 app/views/users/show.html.erb

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>
  <div class="span8">
    <%= render 'follow_form' if signed_in? %>
    <% if @user.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
</div>

这是创建评论迁移

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :user_id
      t.text :body
      t.references :micropost_id

      t.timestamps
    end
    add_index :comments, :micropost_id
  end
end
4

1 回答 1

2

该错误听起来好像您没有进行迁移以将 micropost_id 添加到评论并迁移数据库,所以我将从那里开始。

您的迁移表明您正在使用

t.references :micropost_id

但是 t.references 接受模型名称,而不是foreign_key,因此将其更改为

t.references :micropost

或者更改对 micropost_id 的整数的引用,或者应该给你你想要的结果。

修复迁移后,您可能需要将rake db:reset所有内容恢复到您想要的位置。

于 2012-05-24T02:36:42.033 回答