2

私はRailsを初めて使用し、Railscast #258に従ってjQuery TokenInputを実装していますが、何らかの理由で新しいレコードを作成しようとするとエラーが発生します:

NameError: undefined local variable or method `through' for #<Class:0x101667ef0>
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/base.rb:1008:in `method_missing'
    from /Users/Travis/Desktop/YourTurn/app/models/tag.rb:4
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:453:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:491:in `load_missing_constant'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:183:in `const_missing'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:181:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:181:in `const_missing'

マイタグコントローラー:

class TagsController < ApplicationController

def index
    @tags = Tag.where("name like ?", "%#{params[:q]}%")
    respond_to do |format|
      format.html
      format.json { render :json => @tags.map(&:attributes) }
    end
  end

  def show
    @tag = Tag.find(params[:id])
  end

  def new
    @tag = Tag.new
  end

  def create
    @tag = Tag.new(params[:tagging])
    if @tag.save
      redirect_to @tag, :notice => "Successfully created tag."
    else
      render :action => 'new'
    end
  end

タグの方法:

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :taggings
  has_many :questions, through => :taggings
end

タグ付け方法:

class Tagging < ActiveRecord::Base
  attr_accessible :question_id, :tag_id
  belongs_to :question
  belongs_to :tag
end

:through: を使用する方法:

  has_many :taggings
  has_many :tags, :through => :taggings
  attr_reader :tag_tokens

ターミナルで作成するtag = Tagging.newと、適切なエントリが取得されますが、 db migrate にあるタグ名は取得されませんcreate_tags。問題が何であるかを理解するのを手伝ってくれる人はいますか? 他のコードを提供する必要がある場合は、喜んで提供します。

4

1 に答える 1

4

コロンがありません、これ:

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :taggings
  has_many :questions, through => :taggings
end

これである必要があります:

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :taggings
  has_many :questions, :through => :taggings
end

throughに変更されていることに注意してください:throughthroughは変数ですが、:throughシンボルであり、ハッシュを作成するために通常必要なものです。

于 2011-07-21T01:36:04.570 に答える