2014-02-12 88 views
2

因此,我決定在我的RoR博客中構建標籤系統。繼入門官方導軌導後,即時跟進一個來自jumpstartlabs http://tutorials.jumpstartlab.com/projects/blogger.html#i3:-tagging 哪裏我基本上使用「連接表」的方法來映射很多帖子有多個標籤,許多標籤有很多帖子關係StatementInvalid SQLite3 :: SQLException:沒有這樣的列。向博客添加標籤系統時

這被錯誤我得到

ActiveRecord::StatementInvalid in Posts#show 
    Showing /home/nadia/blog/app/views/posts/show.html.erb where line #8 raised: 

    SQLite3::SQLException: no such column: taggings.post_id: SELECT "tags".* FROM "tags"    INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."post_id" = ? 
    Extracted source (around line #8): 


6 <p> 
7  Tags: 
8  <% @post.tags.each do |tag| %> 
9   <%= link_to tag.name, tag_path(tag) %> 
10  <% end %> 
11  </p> 

這是我tags_controller

1 class TagsController < ApplicationController 
2 
3  def show 
4  @tag = Tag.find(params[:id]) 
5  end 
6 end 

post_controller

1 class PostsController < ApplicationController 
2 include PostsHelper 
3 http_basic_authenticate_with name: "Nadia", password: "possum", except: [:index, :show] 
4 
5 def index 
6  @posts = Post.all 
7 end 
8 
9 def show 
10  @post = Post.find(params[:id]) 
11 end 
12 
13 def edit 
14  @post = Post.find(params[:id]) 
15 end 
16 
17 def update 
18  @post = Post.find(params[:id]) 
19 
20   if @post.update(post_params) 
21    redirect_to action: :show, id: @post.id 
22   else 
23    render 'edit' 
24   end 
25 end 
26 def new 
27  @post = Post.new 
28 end 
29 def create 
30  @post = Post.new(post_params) 
31 
32   if @post.save 
33    redirect_to action: :show, id: @post.id 
34   else 
35    render 'new' 
36   end 
37 end 
38 
39 def destroy 
40  @post = Post.find(params[:id]) 
41  @post.destroy 
42 
43  redirect_to action: :index 
44  end 
45 
46  private 
47 
48  def post_params 
49   params.require(:post).permit(:title, :text,) 
50 
51  end 
52 end 

型號:post.rb

1 class Post < ActiveRecord::Base 
    2 
    3  has_many :comments, dependent: :destroy 
    4  has_many :taggings 
    5  has_many :tags, through: :taggings 
    6  validates :title, 
    7    presence: true, 
    8    length: { minimum: 5 } 
    9 
    10 def tag_list 
    11  self.tags.collect do |tag| 
    12  tag.name 
    13 end.join(", ") 
    14 end 
    15 def tag_list=(tags_string) 
    16  tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq 
    17  new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by(name:  name) } 
    18  self.tags = new_or_found_tags 
    19 end 
    20 end 

型號:tag.rb

1 class Tag < ActiveRecord::Base 
2 has_many :taggings 
3 has_many :posts, through: :taggings 
4 end 

和最後但並非最不重要我posts_helpers.rb

1 module PostsHelper 
2 def post_params 
3  params.require(:post).permit(:title, :body, :tag_list) 
4 end 
5 end 

請讓我知道如果你想看到任何其他文件。我會喜歡一些幫助,因爲這是我第一個真正的項目。 :)

編輯。 My Taggings.rb

1 class Tagging < ActiveRecord::Base 
2 belongs_to :tag 
3 belongs_to :post 
4 end 
+0

你的'@ post'可能會返回'nil''tag_list'。爲什麼使用文本字段呢? (請記住'@ post.tag_list'將被調用)。檢查http://apidock.com/rails/ActionView/Helpers/FormHelper/text_field) – rlecaro2

+0

爲了對你非常誠實,我按照說明操作,他們不清楚你爲什麼這麼做。而且我在這個時間點的知識還不足以理解爲什麼要這樣做。 – nadia

+0

'SQLite3 :: SQLException:沒有這樣的列:taggings.post_id'確切地說出了什麼問題。您的標記模型應該有一個'post_id'和'tag_id'來正確引用多對多關係。你的模式說什麼?(我不明白我以前的評論,我想你編輯了錯誤)。 – rlecaro2

回答

6

我在Taggings.rb文件中發現了一個輸入錯誤/複數。

belongs_to :posts 

,我再改爲

belongs_to :post 

然而,重要的部分是我忘了,我不得不重新運行遷移重新命名列的名稱。我通過運行

Tagging.column_names 

發現柱子其實命名錯誤檢查的列名:運行

rake db:migrate 

與固定Tagging.rb文件後posts_id

它解決了這一問題通過重新命名該列。

非常感謝你們每一個 我真的很感謝你的幫助。在過去的幾天裏學到了一堆,試圖解決這個小問題。

+1

+1充滿愛心回答自己。一定要接受它艱難。 – rlecaro2

相關問題