2014-02-12 6 views
2

그래서 RoR 블로그에 태그 시스템을 구축하기로 결정했습니다. 시작하기 공식 레일 가이드를 따른 후, 메신저 나는 기본적으로 많은 게시물이 많은 태그와 많은 태그 관계를 많은 게시물을StatementInvalid SQLite3 :: SQLException : 해당 열이 없습니다. 블로그에 태그 시스템을 추가 할 때

이 되세요 가지고 매핑 '테이블을 조인'방법을 사용하고 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> 

를 오류 받고 있어요된다

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

post_controller 내 tags_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 

날 경우 알려 주시기 바랍니다 다른 파일을보고 싶습니다. 이것이 나의 첫 번째 실제 프로젝트이기 때문에 나는 약간의 도움을 원할 것이다. :)

편집. 내 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'가 호출된다는 것을 기억하십시오). Check at http://apidock.com/rails/ActionView/Helpers/FormHelper/text_field) – rlecaro2

+0

매우 솔직하게 말하면 지침을 따라 갔고, 왜 그렇게했는지는 분명하지 않았습니다. 그리고이 시점에서의 나의 지식은 그 이유를 충분히 이해하지 못합니다. – nadia

+0

'SQLite3 :: SQLException : 해당 열이 없습니다 : taggings.post_id'는 정확히 무엇이 잘못되었는지를 말합니다. taggings 모델은 다 - 대 - 다 관계를 제대로 참조하기 위해'post_id'와'tag_id'를 가져야합니다. 당신의 스키마는 무엇을 말하고 있습니까?(나는 나의 이전의 코멘트를 이해하지 못한다, 나는 당신이 과실을 편집했다고 생각한다). – rlecaro2

답변

6

Taggings.rb 파일에서 오타/복수형을 발견했습니다. 중요한 부분은 내가 열 이름을 다시 이름으로 마이그레이션을 다시 실행했다 잊고 있었다 그러나

belongs_to :posts 

나는 다음

belongs_to :post 

로 변경한다. 이 문제를 해결 고정 Tagging.rb와

rake db:migrate 

실행 파일 후

을 posts_id : 나는 열이 사실 잘못이라는 것을 찾을

Tagging.column_names 

을 실행하여 열 이름을 확인 해당 열의 이름을 다시 지정하십시오.

마다 감사합니다. 정말 도움을 주셔서 감사합니다. 지난 몇 일 동안이 작은 문제를 해결하기 위해 무리를 배웠다.

+1

+1 자신을 대답하기에 충분한 배려. 힘든 것을 받아 들여야합니다. – rlecaro2