令牌字段 - 记录已插入然后已删除

时间:2012-09-01 06:02:22

标签: ruby-on-rails jquery-tokeninput

我正在尝试根据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

我的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

我的活动表格:

<%= 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
  });
});

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

1)你有在任何地方命名的before_filter吗?

2)你有:在attr_accessible中命名的activities_tags吗?