1

だから私はブログを作っていて、acts_as_taggable_onそれぞれentryを 、:category:subcategoryに使おうとしています:topicentryモデル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ますか?



entry.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