1

所以我正在制作一个博客,并尝试将acts_as_taggable_on每个entry用于:category,:subcategory:topic. entry模型belongs_to :useracts_as_ordered_taggable/acts_as_ordered_taggable_on :category, :subcategory,。_

entries_controller.rb update是:

@entry = Entry.find_by_id(params[:id])
if @entry.update_attributes(params[:entry])
  redirect_to entries_path
else
  render "edit"
end

当我提交对条目的编辑时,我收到此错误:

> NoMethodError Exception: undefined method `each' for "test_category":String

在这种情况下,这String 'test_category'就是我编辑的值:category

这有关系acts_as_taggable_on吗?



入口.rb


class Entry < ActiveRecord::Base
  belongs_to :user

  # Paperclip
  has_many :attached_assets, :dependent => :destroy
  accepts_nested_attributes_for :attached_assets, :allow_destroy => true 

  # Acts_As_Taggable
  acts_as_ordered_taggable
  acts_as_ordered_taggable_on :category, :subcategory, :topic

  validates_presence_of :title, :subtitle, :category, :post, :subcategory, :topic
  attr_accessible :title, :subtitle, :category_list, :post, :subcategory_list, :topic_list, :asset, :asset_file_name, :asset_type, :entry_id

  delegate :title?, :subtitle?, :category?, :post?, :subcategory?, :topic?, :to => :user

  before_save :to_l

  private

    def to_l
      self.category.downcase
    end
end


entry_controller.rb


class EntriesController < ApplicationController
  before_filter :authenticate_action

  def index
    @entries = Entry.order("created_at desc")
  end

  def new
    @entry = Entry.new
  end

  def show
    @entry = Entry.find_by_id(params[:id])
  end

  def create
    @entry = Entry.new(params[:entry])

    if @entry.save
      redirect_to entries_path
    else
      render "new"
    end
  end

  def edit
    @entry = Entry.find_by_id(params[:id])
  end

  def update
    @entry = Entry.find_by_id(params[:id])
    debugger
    if @entry.update_attributes(params[:entry])
      redirect_to entries_path
    else
      puts @entry.errors.messages.inspect
      render "edit"
    end
  end

  def destroy
    @entry = Entry.find_by_id(params[:id])
    @entry.destroy
    redirect_to entries_path, :notice => "#{@entry.title} has been deleted"
  end
end


WEBrick 输出


> /Users/kyle/Projects/blog/app/controllers/entries_controller.rb:33
> if @entry.update_attributes(params[:entry])
> (rdb:135) 
> {:category=>["can't be blank"], :subcategory=>["can't be blank"], :topic=>["can't be blank"]}


> Started PUT "/entries/4" for 127.0.0.1 at 2012-07-19 11:29:43 -0500
> Processing by EntriesController#update as HTML
  > Parameters: {"utf8"=>"✓&quot;, "authenticity_token"=>"47zbADuuFS3xC5RFc6nLR7qUnE2bn1MZoNm0IwESCcI=", "entry"=>{"title"=>"Test 4", "subtitle"=>"Just Kidding!", "category_list"=>"test_category new", "subcategory_list"=>"test_subcategory new", "topic_list"=>"test_topic new", "post"=>"I pulled a George Lucas, sploops! \r\n\r\njfk;dajgk;dasjkgl;dasczcvzcVzcxv"}, "commit"=>"Update Entry", "id"=>"4"}
  > User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
  > Entry Load (1.1ms)  SELECT "entries".* FROM "entries" WHERE "entries"."id" = 4 LIMIT 1
   > (0.2ms)  BEGIN
  > ActsAsTaggableOn::Tag Load (0.7ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Entry' AND (taggings.context = 'category' AND taggings.tagger_id IS NULL) ORDER BY taggings.id
  > ActsAsTaggableOn::Tag Load (0.9ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Entry' AND (taggings.context = 'subcategory' AND taggings.tagger_id IS NULL) ORDER BY taggings.id
  > ActsAsTaggableOn::Tag Load (1.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Entry' AND (taggings.context = 'topic' AND taggings.tagger_id IS NULL) ORDER BY taggings.id
  > ActsAsTaggableOn::Tag Load (0.7ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Entry' AND (taggings.context = 'category') ORDER BY taggings.id
  > ActsAsTaggableOn::Tag Load (0.8ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Entry' AND (taggings.context = 'subcategory') ORDER BY taggings.id
  > ActsAsTaggableOn::Tag Load (0.7ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 4 AND "taggings"."taggable_type" = 'Entry' AND (taggings.context = 'topic') ORDER BY taggings.id
   > (0.2ms)  ROLLBACK
  > Rendered entries/_form.html.haml (49.3ms)
  > Rendered entries/edit.html.haml within layouts/application (53.0ms)
> Completed 200 OK in 9380ms (Views: 91.8ms | ActiveRecord: 6.9ms)


4

1 回答 1

2

好的,我的工作正常了。

首先,我将出现的:category:subcategory替换:topic:category_list:subcategory_list:topic_listvalidates_presence_ofattr_accessible


** 警告:acts-as-taggable-on仍然需要拥有acts-as-[ordered-]taggableacts-as-[ordered-]taggable-on :attr成为原始列(即:category:subcategory:topic)才能正常运行!


你有没有看到警告:上面?好的。

然后我去 my_form.html.haml并替换了属性:category:subcategory:topic为 myf.text_field替换为:category_list,:subcategory_list:topic_list

就是这样!事情现在进展顺利!


另外,仅供参考,我配置acts-as-taggable-onapplication.rb

ActsAsTaggableOn.delimiter = ' ' # use space as delimiter
ActsAsTaggableOn.remove_unused_tags = true
ActsAsTaggableOn.force_lowercase = true

更新


我决定做事要简单得多的方法是:tags为我的 ...tags 调用一个属性,而不是让三个单独的字段“充当可标记”。所以,这是我在回答这篇文章后所做的:


删除不需要的表


kyle-/blog: rails g migration RemoveCategoryFromEntry category:string
      invoke  active_record
      create    db/migrate/20120719214435_remove_category_from_entry.rb

kyle-/blog: rake db:migrate
==  RemoveCategoryFromEntry: migrating ========================================
-- remove_column(:entries, :category)
   -> 0.0141s
==  RemoveCategoryFromEntry: migrated (0.0143s) ===============================

kyle-/blog: rails g migration RemoveSubcategoryFromEntry subcategory:string
      invoke  active_record
      create    db/migrate/20120719214845_remove_subcategory_from_entry.rb
kyle-/blog: rake db:migrate
==  RemoveSubcategoryFromEntry: migrating =====================================
-- remove_column(:entries, :subcategory)
   -> 0.0024s
==  RemoveSubcategoryFromEntry: migrated (0.0025s) ============================

kyle-/blog: rails g migration RemoveTopicFromEntry topic:string
      invoke  active_record
      create    db/migrate/20120719214922_remove_topic_from_entry.rb
kyle-/blog: rake db:migrate
==  RemoveTopicFromEntry: migrating ===========================================
-- remove_column(:entries, :topic)
   -> 0.0020s
==  RemoveTopicFromEntry: migrated (0.0021s) ==================================

kyle-/blog: rails g migration AddTagsToEntry tags:string
      invoke  active_record
      create    db/migrate/20120719215013_add_tags_to_entry.rb
kyle-/blog: rake db:migrate
==  AddTagsToEntry: migrating =================================================
-- add_column(:entries, :tags, :string)
   -> 0.0039s
==  AddTagsToEntry: migrated (0.0040s) ========================================

入口.rb


class Entry < ActiveRecord::Base
  belongs_to :user

  # Paperclip
  has_many :attached_assets, :dependent => :destroy
  accepts_nested_attributes_for :attached_assets, :allow_destroy => true 

  # Acts_As_Taggable
  acts_as_ordered_taggable
  acts_as_ordered_taggable_on :tags

  validates_presence_of :title, :subtitle, :post, :tag_list
  attr_accessible :title, :subtitle, :tag_list, :post, :asset, :asset_file_name, :asset_type, :entry_id

  delegate :title?, :subtitle?, :tags?, :post?, :to => :user
end

条目/_form.html.haml


- if @entry.errors
  - @entry.errors.messages.each do |message|
    = message

= form_for @entry, :url => entry_path, :html => {:multipart => true} do |f|
  = f.label :title, "Title"
  = f.text_field :title
  %br
  = f.label :subtitle, "Subtitle"
  = f.text_field :subtitle
  %br
  = f.label :tag_list, "Tags"
  = f.text_field :tag_list
  %br
  = f.label :asset
  = f.file_field :asset, :html => {:multiple => true, :name => "entry[attached_assets_attributes][][assset]"}
  %br
  = f.label :post, "Post"
  = f.text_area :post
  %br
  = f.submit

条目/show.html.haml


%h1= @entry.title
%h2= @entry.subtitle
%h3= @entry.tag_list
%h4= @entry.updated_at
%entry
    = @entry.post

= link_to "Back", entries_path

= render "disqus"

条目#创建


def create
  @entry = Entry.new(params[:entry])

  if @entry.save
    redirect_to entry_path(@entry), :notice => '#{@entry.title} has been created'
  else
    puts @entry.errors.messages.inspect
    render 'new'
  end
end

application.rb 更改


我已经删除了ActsAsTaggableOn.delimiter = ' 'ActsAsTaggableOn.remove_unused_tags = true因为我决定毕竟要使用逗号作为分隔符,因为acts-as-taggable-on正在删除我正在测试的标签。

# acts-as-taggable-on
ActsAsTaggableOn.force_lowercase = true
于 2012-07-19T17:07:30.423 回答