我看過很多教程,以及如何在rails中創建多態關聯但不能使其工作。截至目前,我下面就多態關聯Ryan Bates tutorial,我不斷收到對多義關聯導軌缺少路徑助手3
錯誤未定義的方法我一個試圖標籤添加到我的博客文章,不想使用插件
我收到錯誤
undefined method `tags_path' for <class>
的routes.rb
resources :blog do
resources :tags
end
_form.html.erb標籤爲
<%= form_for([@taggable, @tag]) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
tag.rb
class Tag < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
end
blog.rb
class Blog < ActiveRecord::Base
has_many :tags, :as => :taggable
end
遷移文件
class CreateTags < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.string :name
t.string :taggable_type
t.integer :taggable_id
t.timestamps
end
end
def self.down
drop_table :tags
end
end
tags_controller.rb
def index
@taggable = find_taggable
@tags = @taggable.tags
end
def find_taggable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
raise ActiveRecord:NoRecord.new("Couldn\'t find it captain!")
end
Rails的是什麼版本的?這篇教程已經快三年了,這在Rails時代已經很長時間了。我所知道的最好的指南是[Rails Guide](http://guides.rubyonrails.org/association_basics.html),它涵蓋了多態關聯,但值得一讀的是它的(簡短)整體。 – 2012-02-07 02:29:31
rails 2.另外我知道它是舊的,但其中一些概念與rails 3中的一樣,只是試圖找出錯誤出現的原因。生病檢查你已經發送的鏈接我只在目前的軌道api中查找 – coletrain 2012-02-07 02:38:23