Jump to content

The ultimate community for Ruby on Rails developers.


Photo

StatementInvalid error when adding a tagging system to a blog


  • Please log in to reply
No replies to this topic

#1 nadia

nadia

    Passenger

  • Members
  • 1 posts

Posted 13 February 2014 - 06:21 AM

Hi there, I'm very new to RoR please be kind.d

I wasn't sure where to go but I was hoping I could find help here after days of hair pulling. Stackoverflow was not successful. 

I have followed the Official Beginners Guide to ROR and made a blog.

I wanted to take it further and add a tagging system to the blog. Im getting errors and I don't have enough knowledge of ruby to determine what is causing them. Any thoughts/help would be really appreciated.

So this is the error 

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):

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

 

 

And this a small chunk of the 

post_controller: 

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

This is my tag_controller


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

my tag.rb

  1 class Tag < ActiveRecord::Base
  2   has_many :taggings
  3   has_many :posts, through: :taggings
  4 
  5 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
~       

and my posts_helper

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

Please let me know if I can add any more code that you would need to see

Please please help. I really need to get out of this little rut. And any comments would be appreciated.

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users