我已经查看了许多教程,而不是如何在rails中创建多态关联,但似乎无法使其正常工作。截至目前,我正在关注多态关联的Ryan Bates tutorial,并且我一直在为
获取错误未定义的方法我试图在我的博文中添加标签而不想使用插件
我收到错误
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