2014-02-12 32 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

相关问题