2

act_as_tggable プラグインを使用して、Ruby on Rails アプリケーションにタグ機能を含めようとしています。コードを添付しました。プラグインをインストールし、移行も実行しました。次のエラーが表示されます。

undefined method `each' for "value of the parameter":String

コード

location.rb - ロケーションテーブルには名前、タグがあります (これはテーブルにある追加フィールドです。プラグインについて知る前に追加しました:)、都市フィールド

class Location < ActiveRecord::Base
  belongs_to :profile
  acts_as_taggable
end

profile.rb

class Profile < ActiveRecord::Base
  has_many :locations
  acts_as_tagger
end

location_controller.rb

def create
  @location = Location.new(params[:location])
  @location.tag_list = ["tags1","tags2"]
  if @location.save
     redirect_to(@location)
  else
     redirect_to(@profile)
  end 
end

アプリケーション トレース

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:320:in `replace'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/associations.rb:1331:in `block in collection_accessor_methods'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `block in assign_attributes'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in `attributes='
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2473:in `initialize'
/Users/felix/rails_projects/sample_app/app/controllers/locations_controller.rb:92:in `new'
/Users/felix/rails_projects/sample_app/app/controllers/locations_controller.rb:92:in `create'

ありがとう

4

4 に答える 4

3

ひょっとして Ruby 1.9 をお使いですか?この回答の残りの部分は、「はい」で始まります。もしそうなら、読み進めてください。

1.9 の動作変更につまずいたかもしれません。1.9 の文字列はサポートされなくなりましたeach(つまり、EnumerableRuby 1.8 とは異なります)。each_charしかし、おそらく意図されたものを使用できます。

これがあなたのコードの爆発ではない場合は、次のいずれかを実行できます。

  • 1.8.x に戻る (明らか)
  • メソッドを追加して String クラスをハックしますeach(面倒で危険な可能性があります)
  • 問題の原因となっている gem またはプラグインを修正します。

ここにすべてに関する素晴らしい記事があります

于 2010-07-20T21:54:25.357 に答える
0

私はacts_as_taggableで未定義の各メソッドについて不平を言う同様の問題を抱えていましたが、問題はview _formに間違ったフィールドがあったことでした。私が持っていた:

<%= f.text_field :tags %>

本来あるべきものの代わりに:

<%= f.text_field :tag_list %>
于 2012-04-04T21:09:54.647 に答える
0

で置き換え@location.tag_list = ["tags1","tags2"] て みてください

@location.tag_list = "tags1, tags2"

また、次のようにタグを追加することもできます

@location.tag_list.add("tag1, tag2", parse: true)

詳細については、このキャストを確認してください

于 2015-07-17T13:28:21.567 に答える
0

params の値が何であるかわからないので、私は推測しています (おそらく、それらを取得するために aparams.inspectを出力できます)。params[:location] で Location.new に渡した値は文字列であり、キーと値のペアのハッシュを期待していたと思います。

多分あなたは意味した:Location.new(:location => params[:location])

または:Location.new(params)

または、Location.new(params[:location]) 正しいかもしれませんが、本来あるべきハッシュではありません (これは通常、ビュー コードのフォーム ヘルパーによって行われます)。

于 2010-07-20T21:47:40.537 に答える