Создать новую запись получает NameError: неопределенная локальная переменная или метод 'through' - PullRequest
2 голосов
/ 21 июля 2011

Я новичок в 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'

Контроллер My Tags:

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

Метод с: через:

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

Если я нахожусь в Терминале и создаю tag = Tagging.new, я получаю соответствующие записи, но не имя тега, которое есть в db migrate create_tags. Может кто-нибудь помочь мне разобраться в чем проблема? Если мне нужно предоставить другой код, я буду рад.

1 Ответ

4 голосов
/ 21 июля 2011

Вам не хватает двоеточия, это:

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 изменилось на :through.Просто through является переменной, но :through является символом, и это то, что вы обычно хотите для построения хэшей.

...