How to implement the tags using acts_as_taggable_on_steroids

Here is the my application details:

work environment:windows
instantrails2.0

1.i have created a controller and a model for ‘aticle’ and
‘announcements’.

2.i added acts_as_taggable in both the models(i.e article.rb and
announcement.rb)

3.i created a migration for tag support like add_tag_support.rb which
contains the two tables ,names ‘tags’ and ‘taggings’

class AddTagSupport < ActiveRecord::Migration
def self.up
# Table for your Tags
create_table :tags do |t|
t.column :name, :string
end

create_table :taggings do |t|
  t.column :tag_id, :integer
  # id of tagged object
  t.column :taggable_id, :integer
  # type of object tagged
  t.column :taggable_type, :string
end

end

def self.down
drop_table :tags
drop_table :taggings
end

4.i created a migration for the two table article and announcements
for tag support like:

class AddArticlesAddAnnouncements < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.column :name, :string
t.column :body, :string
t.column :created_on, :date
t.column :updated_on, :date
end
create_table :announcements do |t|
t.column :body, :string
t.column :created_on, :date
t.column :updated_on, :date
end
end

def self.down
drop_table :articles
drop_table :announcements
end
end

4.then i did
rake db:migrate

5.if i login to
http://localhost:3000/articles
or
http://localhost:3000/announcements

am able to enter the data into both tables

6 then i enter to console window

Artical.tag_with(‘xxxx’)
if i do the above command im getting error like method is not defined
article = Article.find(1)
is working
article.tag_list
is giving empty array like => [ ]

below are my quires:

1)can anyone help out me in this issue how to tag the tables and is
there any changes do i need to change.
2)i try to instal steroids like:
ruby script/plugin install
http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
its giving like no plugin found so i downloaded and just copied in to
vendor/plugins/acts_as_taggable_on_steroids
and started working .is it wrong?
3)where we need to do the changes in controller and views to search
for perticular tag from the two models for user interface?

thanks

hi srikanth,

I just checked this against my implementation of the same plugin.
As far as I can see, you did everything right.
The plugin is loading, otherwise the acts_as_taggable in
the model would give you an error.

Only difference:
I used:
@article.tag_list = “xxx”
instead of tag_with, which doesn’t exist in this plugin.
(Not sure, I think it did exist in the old acts_as_taggable plugin,
so you may find it in some tutorials)

hi Thorsten,

thanks for ur reply.

Yeah u r right.i have found that tag_with is not working .

suppose i want to find the data from articles and announcements using
tag how can i do that.

article table is having fields:
name:string
body:string

announcements table having fields:
body:

am able to enter the data from html page

now i want to check tag function so how can i do that .
can u help n this where i need to chane in controller and views

thanks

We didn’t use too much of the find functions, since
we used sphinx full text search on the database and just
included the tags in the sphinx index.

the basic function to get articles with given tags:

@article = Article.find_tagged_with(“something”)

This takes a few options:
Pass either a tag, string, or an array of strings or tags.
:exclude - Find models that are not tagged with the given tags
:match_all - Find models that match all of the given tags, not just
one
:conditions - A piece of SQL conditions to add to the query

On the first view you will need a form to enter the search like:

<% form_tag(search_articles_path(), :method => :get) do %>
<%= label(:search, :tag, “Search:”) %>



<% end %>

this would give the search in params[:search]

depending on some details, you can use this directly like
@articles = Article.find_tagged_with(params[:search])

But there are many ways to do this and much depends on details,
what exactly to search and display.
Some Ajax response may be another option.
But can’t help you there, since that would require more knowledge
about the needs and actual state of your site.

hi,

from the above code,
what i understud is
search is a method and it shold be defined in the controller right like
def search
@articles = Article.find_tagged_with(params[:search])
end
or how it will be exactly can u help me regarding this

thanks
.
and
to enter tag as search parameter on first form we should have some
text box to enter right.how to do it.

hi,

that commond is working on console,but i want give the tag name from
html and it should take the tag paramaer and able to list out the tags
which r linking to article and announcements.For this w need to update
the controller of article and announcements and at the same time we
need to update the views like
index.html.erb,show.html.erb,new.html.erb,edit.html.erb files .

so here im getting stuck :
1)how to give the tag input from the html file (it works like search
for all the taggables items from database)
2)where exatly i need to change the controller and views

if u have any idea any sample application let me know

thanks

hi ,

sorry for disturbing u .

can u explain wat that code u have given will do,
i added that coode in index.html.erb
but im gettin unintialized method error:it is expected because the
method i did not added in controller so can u give dea where can i
modify the code
search_articles_path(),

thanks

hi thanks for that

in code snippet :
<% form_tag(search_articles_path(), :method => :get) do %>
<%= label(:search, :tag, “Search:”) %>



<% end %

my quieries:
1):method => :get
the above method shoud be defined in controller right.
2)above code shoud take tag parameter right so from where it is
getting the tag parameter to search the database?

can u clarify this.

thank u very much

search_articles_path would be generated by RESTful routing like (in
routes.rb):

map.resources :articles, :colection => [:search]

but you can use any other path helper instead or a string with the url

1):method => :get
the above method shoud be defined in controller right.

no, the :method => :get relates to form methods in html, nothing to do
with methods in Rails
if you defined the routes like
map.resources :articles, :collection => [:search]
and use the path helper search_articles_path, then the method must be
in
controler: Articles
method: search

2)above code shoud take tag parameter right so from where it is
getting the tag parameter to search the database?

the text entered by the user in the form will be in
params[:search]

hi,

You mean to say i need to add

1)map.resources :articles, :collection => [:search] in route.rb

2)do i need to add method in controller like

def get

end

3)Can we give the tag parameter as text box entry from the
forms(index.html.erb) and that tag parameter can be searched in the
database?

can u clear on this

You mean to say i need to add
1)map.resources :articles, :collection => [:search] in route.rb

yes

2)do i need to add method in controller like

def get

end

no, it should be:
def search

end

the :collection => [:search]
declares the existence of this search method in the articles
controller

3)Can we give the tag parameter as text box entry from the
forms(index.html.erb) and that tag parameter can be searched in the
database?

yes, if you have a textbox named “search”, then
params[:search]
will have the text entered in this box. You
can give it any other name of course, params will have the value
with the same name.
(You can see what params are returned in the development.log)

To use this for searching:
@articles = Article.find_tagged_with(params[:search])

then you should have all artcles with that tag in @articles
and can display them in the view

Hi Thorsten,

really very helpful ur ideas to me as i am very new to rails and sql.

I HAVE UPDATED MY APPLICATION LIKE BELOW as per ur suggetions:

1)I added this search :map.resources :articles, :collection =>
[:search] in route.rb like below

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
#added for search
map.resources :articles, :collection => [:search]
end
2)i defined a search method in article controller like below:

def search
@articles = Article.find_tagged_with(params[:search])
end

3)i added the below code in idex.html.erb as u said and added a
commond to get a search box for tag entry.

Search:

<% form_tag(search_articles_path(), :method => :get) do %>
<%= label(:search, :tag, “Search:”) %>



<% end %>

then i trid http://localhost:3000/articles

i am able to see on index page like below list:

Listing articles
Name Body
Cricket india vs pak tour Show Edit Destroy
politics HM quits Show Edit Destroy

New article

Search:

from the index page i am able to do show,edit,destory and new article
functionalities and i am getting the search box to enter a tag name.

then my quiries:
1)if i enter some tag name then i press the search button i m getting
erors like:

ActiveRecord::RecordNotFound in ArticlesController#show
Couldn’t find Article with ID=search
RAILS_ROOT: E:/srikanth/InstantRails-2.0-win/rails_apps/blog
RequestParameters:
{“search”=>“fun”,
“x”=>“48”,
“y”=>“18”,
“id”=>“search”}

2)if the search isable to do searching then the reasult shoud be list
out on index rt.so for this do we need update in any one of views(to
get the results) can u tell me which view i ned to modify and how ?

3)small doubt:

i have a table like contacts with columns like:

id name age location
1 a 12 ap
2 b 13 ap
3 c 12 up

corresponding tag table like:
id name
1 xx
2 yy
3 zz

here my douts are
1)the tag name shoud be one of the parameter like name or age or place
or it should be the entry name of perticuler column?
here actaully i given tag name as fun and serious as tag names but we
did not mention any relation table contents of article ,even if i do
article.find_taggable_with(‘fun’) it is giving the details of teble
entrys.i did not understand the logic here can u help in this?

thanks
Srikanth
2)suppose i want to find all people from place ap i want to search
,how can i do that?
3)how the taging table is updating with all the above columns in the
tagging table?

tagging table like:
id tid taggable_id taggable_type

hi thorsten,
actually i got stuck here itself i am waiting for ur reply if you
could do that its very help ful to me

thanks

hi thorsten

on console i have tried below commonds:

Article.find(1)
working fine

article.tag_list
giving error like unfdefined local variable or method article
But if i check the tas table from mysql prompt i am able to see the
tag names like xx,yy…

i tried to give the tag names like

article.tag_list = "fun serious "
here also giving the same error but i trie yester in the same way it
showned like
[“fun”,“serious”]

can u tell me how togive the tag name on console without using
article.tag_with method because this method not working.

thanks

On 4 Dec 2008, at 06:55, sreekanth.G wrote:

hi thorsten

on console i have tried below commonds:

Article.find(1)
working fine

article.tag_list
giving error like unfdefined local variable or method article

Well yes - you haven’t defined article.
if you do article = Article.find(1)
then you should be able to play around with article.tag_list etc…

Fred

On 4 Dec 2008, at 13:10, sreekanth.G wrote:

Hi

i am clear abot that but
if i write below

article_tag_with(‘tagname’)
undefined methos article
how can i do this?
thanks

Well that’s the same thing. you’ve got to set article to be an
instance of article before you can call article.tag_with ‘foo’

Fred

Hi

i am clear abot that but
if i write below

article_tag_with(‘tagname’)
undefined methos article
how can i do this?
thanks

On Thu, Dec 4, 2008 at 6:37 PM, Frederick C.