RailsCasts エピソード #258 に基づいてトークン フィールドを機能させようとしていますが、フォームを問題なく送信できます。メイン ページに戻ると、タグが表示されません。
ローカルマシンのサーバーログを見ると...
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
「INSERT INTO "activities_tags"...」には正しい値がありますが、何らかの理由でレコードがすぐに削除され、それがどこに影響を与えるのかわかりません。
私のアクティビティコントローラー:
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
私のアクティビティ.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
私の活動フォーム:
<%= 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 %>
最後に、私の activities.js:
$(function() {
$("#activity_tag_tokens").tokenInput("/tags.json", {
crossDomain: false,
prePopulate: $("#activity_tag_tokens").data("pre"),
theme: "facebook",
allowCustomEntry: true
});
});
何かご意見は?