语句无效的 SQLite3::SQLException:没有这样的列.向博客添加标记系统时



所以我决定在我的RoR博客中建立一个标记系统。在遵循入门官方轨道指南后,我将跟进来自 jumpstartlabs http://tutorials.jumpstartlab.com/projects/blogger.html#i3:-tagging我基本上使用"连接表"方法来映射许多帖子有很多标签和许多标签有很多帖子关系

的地方

这是我收到的错误

  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

型号: 后.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

型号: 标签.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

如果您想查看任何其他文件,请告诉我。我希望得到一些帮助,因为这是我的第一个真正的项目。:)

编辑。 我的标记.rb

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

我在Taggings.rb文件中发现了一个拼写错误/复数。

 belongs_to :posts

然后我改成了

 belongs_to :post

但是,重要的部分是我忘记了我必须重新运行迁移才能重命名列的名称。我通过运行检查了列名

 Tagging.column_names

发现该列实际上命名错误:posts_id

运行后

 rake db:migrate

使用固定的 Tagging.rb 文件,它通过重命名该列来解决问题。

非常感谢大家我真的很感谢你的帮助。在过去的几天里,我学到了很多东西,试图解决这个小问题。

最新更新