Search implimentation for tags

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

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

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).

<% 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.

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