Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:36:43+00:00 2026-06-14T00:36:43+00:00

I am trying to generate a tagging system similar to SO. I am using

  • 0

I am trying to generate a tagging system similar to SO. I am using the Select2 gem. When the user initially goes to the new page the form should only display their tags. When on the page they can create new tags by typing out tag names and separating them by spaces or commas.

My issues is that when I go to submit this form, the tags are not properly linking to an ID. I get the error “Couldn’t find Tag with id = 0” or if I have the number 12 as the tag, “Couldn’t find Tag with id = 12”

A user has_many tags; a post has many taggings; a post has many tags through taggings; a tag has many taggings; a tag has many posts through taggings; a tag belongs to an user

How could I specify the tag id name and only have the tag name show up?

My Tags Controller looks like this

  respond_to :json
  def index
    @tags = current_user.tags
    respond_with(@tags.map{|tag| {:id => tag.id, :name => tag.name, :user_id => tag.user_id} })
  end

My JavaScript looks like this

var items = [];
$.getJSON('http://localhost:3000/tags.json', function(data) {
        $.each(data, function(i, obj) {
             return items.push(obj.name);
        });
   $("#post_tag_ids").select2({
        tags: items,
        tokenSeparators: [",", " "]
        });
   });

My form looks like this

= semantic_form_for([@user, @post], :html => { :class => 'form-horizontal' }) do |f|
  = f.inputs do
    = f.input :summary, :label => false, :placeholder => "Title"
    = f.input :tag_ids, :as => :string, :label => false, :placeholder => "Tags", input_html: {:id => "post_tag_ids", :cols => 71}
  = f.buttons do
    .action
  = f.commit_button :button_html => { :class => "btn btn-primary" }

My Post Controller looks a little like this

def create
 @post = Post.new(params[:post])
 @post.user = current_user
 @post.save
 @post.tag!(params[:tag_ids], current_user.id)
end

My Post Model has a tag! method

def tag!(tags, user)
  tags = tags.split(",").map do |tag|
    Tag.find_or_create_by_name_and_user_id(tag, user)
  end
self.tags << tags
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:36:44+00:00Added an answer on June 14, 2026 at 12:36 am

    The trick to solve this was to set up setter methods in the model. I eventually used something like this :

      def tag_ids=(tags_string)
        self.taggings.destroy_all
        tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
        tag_names.each do |tag_name|
          tag = Tag.find_or_create_by_name_and_user_id(tag_name, self.user.id)
          tagging = self.taggings.new
          tagging.tag_id = tag.id
        end
     end
    

    And in the controller I had to merge a user key into the form params hash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to generate a color gradient using ColdFusion. My current code below works
I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
Im am trying to generate interfaces of HP Quality Center OTACLien.dll using com4j. I
I am trying to generate code on every build of my project using VS2012.
I'm trying to generate a Java Client with the axis using wsdl2java (wrapped by
I am trying to generate script for my sql local database created using visual
While trying to generate a pdf using ExpertPdf.HtmlToPdf, it's using a lot of memory.
I'm trying to generate a list of events that a user is attending. All
I'm trying to generate a ParameterizedType using Guice's Types library. Butler is an inner
Im trying to generate a week view calendar using codeigniter's calendar library.. something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.