我看过许多关于如何在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
endblog.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
endtags_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发布于 2012-02-07 10:38:50
您可以使用polymorphic_url、details
polymorphic_url([@blog, @tag])或者只使用像https://github.com/mbleigh/acts-as-taggable-on这样的标记gem
https://stackoverflow.com/questions/9170187
复制相似问题