2016-01-11 31 views
-1

不知道我在這裏與我的Rails應用程序丟失了什麼。我以非常相似的方式設立了「職位」和「項目」。但是,當我點擊我的「項目」鏈接時,我得到一個ActiveRecord :: Statement無效的錯​​誤。沒有這樣的問題與帖子索引頁面。具體的項目索引頁的信息是:獲取ActiveRecord :: Statement無效錯誤

「的SQLite3 ::的SQLException:沒有這樣的列:CREATED_BY:選擇 」項目工程 「ORDER BY CREATED_BY DESC」

我知道這往往是「 * FROM」。誤報事物造成的結果 - 特別是複數與單數需要匹配。儘管如此,這兩個模型和控制器對我來說也是一樣 - 只有很小的差異。所以我找不到問題所在。

這裏是我的控制器頁:

class ProjectsController < ApplicationController 
    before_action :find_project, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user!, except: [:index, :show] 

    def index 
    @projects = Project.all.order("created_by DESC") 
    end 

    def new 
    @project = Project.new 
    end 

    def create 
    @project = Project.new project_params 

    if @project.save 
     redirect_to @project, notice: "Nice work! Successfully saved!" 
    else 
     render 'new' 
    end 
    end 

    def show 

    end 

    def edit 

    end 

    def update 
    if @project.update project_params 
     redirect_to @project, notice: "Nice work! That project was successfully updated!" 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @project.destroy 
    redirect_to projects_path 
    end 

    private 

    def find_project 
    @project = Project.friendly.find(params[:id]) 
    end 

    def project_params 
    params.require(:project).permit(:title, :description, :link, :slug) 
    end 
end 

而這裏的項目index.html.erb頁:

<h1 id="page_title">Projects</h1> 

<div id="posts_wrapper" class="skinny_wrapper"> 
    <% @projects.each do |project| %> 
     <div class="post"> 
      <p class="date"><%= project.created_at.strftime('%A, %B, %d') %></p> 
      <h2><a href="#"><%= link_to project.title, project %></a></h2> 
      <hr> 
     </div> 
    <% end %> 
</div> 

這是我的_create_projects.rb表信息:

class CreateProjects < ActiveRecord::Migration 
    def change 
    create_table :projects do |t| 
     t.string :title 
     t.text :description 
     t.string :link 

     t.timestamps null: false 
    end 
    end 

我也有這個:

class AddSlugToProjects < ActiveRecord::Migration 
    def change 
    add_column :projects, :slug, :string 
    add_index :projects, :slug, unique: true 
    end 
end 
+1

你在'projects'表'created_by'列? – Pavan

+0

我會將表列信息添加到我原來的帖子中。 – Muirik

+0

啊,是的沒有'created_by'列。檢查我的答案。 – Pavan

回答

3

我想你會發現,created_by應該created_at

created_atupdated_at在移民創建的域當您在遷移文件中有

t.timestamps 

因此改變你的index方法

def index 
    @projects = Project.all.order("created_at DESC") 
    end 
+0

啊!這就是我錯過的。我的帖子控制器有正確的,但不是我的項目控制器。非常感謝。我做了編輯,現在它可以工作。非常感激!! – Muirik

1

的SQLite3 ::的SQLException:沒有這樣的列:CREATED_BY:選擇 「項目」 * 從 「項目」 ORDER BY CREATED_BY DESC

該錯誤清楚地表明在projects表中沒有稱爲created_by的列。

您需要生成遷移以將created_by列添加到projects表。

rails g migration add_created_by_to_projects created_by:string 

,然後做rake db:migrate

相關問題