簡體   English   中英

RoR錯誤:SQLite3 :: SQLException:沒有這樣的列:comments.micropost_id:SELECT“comments”。* FROM“comments”WHERE“comments”。“micropost_id”= 2

[英]RoR error: SQLite3::SQLException: no such column: comments.micropost_id: SELECT “comments”.* FROM “comments” WHERE “comments”.“micropost_id” = 2

我正在為用戶開發一個應用程序,每個用戶都在頁面上顯示一組微博。 我正在嘗試為這些微博添加評論。 每次我訪問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

這個錯誤讓人覺得你沒有進行遷移來添加micropost_id來評論和遷移數據庫,所以我從那里開始。

您的遷移顯示您正在使用

t.references :micropost_id

但是,t.references接受模型名稱,而不是foreign_key,因此將其更改為

t.references :micropost

或更改對micropost_id的整數的引用,或者應該給你想要的結果。

您可能需要執行rake db:reset以在修復遷移后將所有內容恢復到所需位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM