# Search implimentation for tags

**URL:** <https://www.ruby-forum.com/t/search-implimentation-for-tags/154939>\
**Category:** Rails\
**Created:** [December 5, 2008, 11:12am UTC](https://www.ruby-forum.com/t/search-implimentation-for-tags/154939 "2008-12-05T11:12:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 5, 2008, 11:12am UTC](https://www.ruby-forum.com/t/search-implimentation-for-tags/154939/1 "2008-12-05T11:12:31Z")

</div>

hi,

i am implementing a search box from index page,search box allow s the  
tag name to be searched and it should display the results on the other  
page.

if enter a tag name in search box once i press the search button i am  
getting error like:

ActiveRecord::RecordNotFound in StreamsController#show  
Couldn’t find Stream with ID=search  
RAILS\_ROOT: E:/srikanth/InstantRails-2.0-win/rails\_apps/stdb  
Application Trace | Framework Trace | Full Trace  
E:/srikanth/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/  
activerecord-2.0.2/lib/active\_record/base.rb:1267:in `find_one' E:/srikanth/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ activerecord-2.0.2/lib/active_record/base.rb:1250:in `find\_from\_ids’  
E:/srikanth/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/  
activerecord-2.0.2/lib/active\_record/base.rb:504:in `find' app/controllers/streams_controller.rb:16:in `show’

corresonding url have like:  
[http://localhost:3000/streams/search?search=352x240](http://localhost:3000/streams/search?search=352x240)

so can any one help me out in this issue.

1)in controller added search method like:

####added for search option  
def search  
search\_criteria = Article.find\_tagged\_with(params[:search])  
if search\_criteria.empty?  
flash[:notice] = “Please enter search criteria”  
@searchresults = “”  
else  
@searchresults = Streams.find  
(:all, :conditions=\>search\_critera)  
end  
respond\_to do |format|  
format.html # show.html.erb  
format.xml { render :xml =\> @stream }  
end  
end  
################

2)and in view/streams/index.html.erb file

\<% form\_tag(search\_streams\_path(), :method =\> :get) do %\>  
\<%= label(:search, :tag, “Search:”) %\>  
  
  
  
\<% end %\>

3)in routes.rb added line;

map.resources :streams, :collection =\> {:search=\>:get}

and my database details given below:

ysql\> show tables;  
----------------------------+  
Tables\_in\_stdb\_development |  
----------------------------+  
schema\_info |  
streams |  
taggings |  
tags |  
----------------------------+  
rows in set (0.00 sec)

ysql\> select \* from streams;  
----±------------------±-----------±----------±----------  
±--------------------±--------------------+  
id | name | resolution | codecname | framerate |  
created\_at | updated\_at |  
----±------------------±-----------±----------±----------  
±--------------------±--------------------+  
1 | dust.mpeg2 | 1920x1080 | mpeg2 | 30fps |  
2008-12-04 16:23:23 | 2008-12-05 13:17:16 |  
2 | amelie.264 | 720x480 | h264 | 25fps |  
2008-12-04 16:23:58 | 2008-12-05 12:50:26 |  
3 | davincieffect.aac | NA | aac | NA |  
2008-12-04 16:24:46 | 2008-12-05 13:19:51 |  
4 | mummy.mpeg2 | 352x240 | mpeg2 | 30fps |  
2008-12-04 16:25:41 | 2008-12-05 14:03:18 |  
----±------------------±-----------±----------±----------  
±--------------------±--------------------+  
rows in set (0.00 sec)

ysql\> select \* from tags;  
----±------------+  
id | name |  
----±------------+  
1 | codecname |  
2 | resolution |  
3 | omap |  
4 | hd |  
5 | omap hd |  
6 | omap,hd |  
7 | dvsdk |  
8 | 352x240 |  
9 | 352x240 264 |  
10 | 264 |  
11 | h264 |  
12 | 1920x1080 |  
13 | 720x480 |  
14 | 25fps |  
15 | aac |  
16 | mpeg2 |  
----±------------+  
6 rows in set (0.00 sec)

ysql\> select \* from taggings;  
----±-------±------------±--------------+  
id | tag\_id | taggable\_id | taggable\_type |  
----±-------±------------±--------------+  
14 | 8 | 4 | Stream |  
17 | 14 | 2 | Stream |  
18 | 13 | 2 | Stream |  
19 | 12 | 1 | Stream |  
20 | 15 | 3 | Stream |  
21 | 16 | 4 | Stream |  
----±-------±------------±--------------+  
rows in set (0.00 sec)

thnaks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 6, 2008, 8:19am UTC](https://www.ruby-forum.com/t/search-implimentation-for-tags/154939/2 "2008-12-06T08:19:03Z")

</div>

Hi,  
I think you have several issues…

srikanth wrote:

> ActiveRecord::RecordNotFound in StreamsController#show  
> Couldn’t find Stream with ID=search  
> RAILS\_ROOT: E:/srikanth/InstantRails-2.0-win/rails\_apps/stdb  
> Application Trace | Framework Trace | Full Trace  
> E:/srikanth/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/  
> activerecord-2.0.2/lib/active\_record/base.rb:1267:in `find_one' E:/srikanth/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ activerecord-2.0.2/lib/active_record/base.rb:1250:in `find\_from\_ids’  
> E:/srikanth/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/  
> activerecord-2.0.2/lib/active\_record/base.rb:504:in `find' app/controllers/streams_controller.rb:16:in `show’

This appears to be a ‘show’ action - not ‘search’.  
I’d say that your GET-request for /streams/search?search=… is being  
mapped to your ‘show’ action - see last comment below.

> ```
> search_criteria = Article.find_tagged_with(params[:search])
> if search_criteria.empty?
> flash[:notice] = "Please enter search criteria"
> @searchresults = ""
> else
> @searchresults = Streams.find
> 
> ```
> 
> (:all, :conditions=\>search\_critera)

Even though the error was in ‘show’, there look to be some errors here  
in ‘search’.

I think Article.find\_tagged\_with will return an array of Article  
models. You haven’t mentioned your Article model - you seem to have a  
Stream model (which you should use in the singular not plural). So  
maybe you mean Stream. You then use this for the :conditions clause  
in Streams.find(…) which is probably not going to work  
because :conditions corresponds to a WHERE-clause in sql. Check the  
docs for the ‘find’ method ([api.rubyonrails.org](http://api.rubyonrails.org)).

> \<% form\_tag(search\_streams\_path(), :method =\> :get) do %\>  
> \<%= label(:search, :tag, “Search:”) %\>  
>   
>   
>   
> \<% end %\>
> 
> 3)in routes.rb added line;
> 
> map.resources :streams, :collection =\> {:search=\>:get}

Just a guess: but did you add ‘:collection’ to  
“map.resources :streams” after setting up this resource? If you did  
and you didn’t reload the routing table (restart you app), then the  
search action won’t take effect and your show action might be used  
instead which might be why you get the error in ‘show’ with id being  
mapped to ‘search’.

> 

HTH

–  
Daniel B.

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 7, 2008, 10:46am UTC](https://www.ruby-forum.com/t/search-implimentation-for-tags/154939/3 "2008-12-07T10:46:28Z")

</div>

Hi Daniel,

actually i have Stream model and in the controller i added like:

def search

> ```
> search_criteria = Stream.find_tagged_with(params[:search])
> if search_criteria.empty?
> flash[:notice] = "Please enter search criteria"
> @searchresults = ""
> else
> @searchresults = Streams.find
> 
> ```
> 
> (:all, :conditions=\>search\_critera)

2)but did you add ‘:collection’  
“map.resources :streams” after setting up this resource?  
i am not clear this qustion.  
just adde the map.resources :streams, :collection =\> {:search=\>:get}  
in route.rb

1. hw can i write an action to display the search result instead of show  
action?

thanks
