0

I am trying to get token fields to work based on the RailsCasts episode #258 and I can submit the form just fine. When I go back to my main page, my tag does not appear.

Looking at the server logs on my local machine, I can see...

User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'zviHK_WgeHLYUAtbKdzyfQ' LIMIT 1
Activity Load (0.1ms)  SELECT "activities".* FROM "activities" WHERE "activities"."id" = ? LIMIT 1  [["id", "6"]]
(0.0ms)  begin transaction
Tag Load (3.1ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT 1  [["id", 1]]
Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "activities_tags" ON "tags"."id" = "activities_tags"."tag_id" WHERE "activities_tags"."activity_id" = 6
(0.3ms)  INSERT INTO "activities_tags" ("activity_id", "tag_id") VALUES (6, 1)
(0.2ms)  DELETE FROM "activities_tags" WHERE "activities_tags"."activity_id" = 6 AND "activities_tags"."tag_id" IN (1)
(1.6ms)  commit transaction

The 'INSERT INTO "activities_tags"...' has the correct values but, for some reason, it is deleting the record immediately and I can't figure out where that would be coming into play.

My Activity controller:

class ActivitiesController < ApplicationController
... 
  def update
@activity = Activity.find(params[:id])
params[:activity][:tag_ids] ||= []

respond_to do |format|
  if @activity.update_attributes(params[:activity])
    flash[:success] = "Activity was successfully updated!"
        format.html { redirect_to master_resumes_url }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @activity.errors, status: :unprocessable_entity }
  end
    end
  end
...
end

My Activity.rb:

class Activity < ActiveRecord::Base
  attr_accessible :end_date, :organization, :position, :start_date, :user_id, 
                  :tag_tokens, :tag_ids
  attr_reader :tag_tokens

  has_and_belongs_to_many :tags
  belongs_to :user

  def tag_tokens=(ids)
temp = ids.split(",")
self.tag_ids = temp[0]
  end
end

My activity form:

<%= form_for(@activity) do |f| %>
  <h4>Choose Tags</h4>
    <%= f.label :tag_tokens, "Tags" %>
    <%= f.text_field :tag_tokens, "data-pre" => @activity.tags.map(&:attributes).to_json %>
        <p><%= link_to "Create A New Tag", new_tag_path %></p>
<% end %>

Finally, my activities.js:

$(function() {
  $("#activity_tag_tokens").tokenInput("/tags.json", {
    crossDomain: false,
prePopulate: $("#activity_tag_tokens").data("pre"),
theme: "facebook",
allowCustomEntry: true
  });
});

Any thoughts?

unlikely_monkey
  • 183
  • 4
  • 15

1 Answers1

0

1) Do you have a before_filter named anywhere?

2) Do you have :activities_tags named in attr_accessible?

neon
  • 2,811
  • 6
  • 30
  • 44
  • I have a before_filter in the activities controller that checks whether the user is signed in. I didn't think that would have any effect. Do you think it might? I don't think I need :activities_tags, do I? That is the name of the join table and I didn't need it before when I was using a checkbox form to tag things. – unlikely_monkey Sep 01 '12 at 15:42