I have looked at many tutorials and what not on how to create a polymorphic association in rails but can't seem to get it working. As of now I am following Ryan Bates tutorial on polymorphic association and I keep getting the error undefined method for
I a trying to add tags to my blog post and do not want to use a plugin
I am getting the error
undefined method `tags_path' for <class>
Routes.rb
resources :blog do
resources :tags
end
_form.html.erb for tags
<%= 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
Migration file
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
you could use a polymorphic_url, details
polymorphic_url([@blog, @tag])
or just use a tagging gem like https://github.com/mbleigh/acts-as-taggable-on