0

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

最后,我的活动.js:

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

有什么想法吗?

4

1 回答 1

0

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

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

于 2012-09-01T09:10:48.533 回答