Find DB items by "title"

I’m having trouble finding then displaying all items in my “posts” table
that match a certain search string.

I have a from field:

<%= start_form_tag :action=> “search” %>

Search

<%= error_messages_for 'post' %>
    <input type=text name="title">
    <input type="submit" value="Search &#187;" class="primary" />

<%= end_form_tag %>

That points to this action in my “post_controller.rb”

def search
@post = Post.find_all_by_title(params[:title])
end

Which I think is assigning the @post variable with the results. But how
exactly can I get them to display? I made a dummy action in the
post_contoller.rb called “show_search” and a view called
“show_search.rhtml” what’s the last step to get the results to display?

in your post_controller.rb:

def show_search
@posts = Post.find_all_by_title(params[:title])
end

in your view:

<% for post in @posts do %>
Post Title:<%= post.title %>
Created At: <%= post.created_at %>
<% end %>

and change the :action in your start_form_tag to be “show_search”
instead of search

Mike

That makes perfect sense. But, I just get a blank page when it comes to
the ruby code. Example:

did this display?

<% for post in @posts do %>
Post Title:<%= post.title %>
Created At: <%= post.date_created %>
<% end %>

That’s in the show_search.rhtml The “did this display?” part shows up,
but nothing below it. I double checked to make sure the form action tag
is now set to show_search, and it is. Any ideas?

Jasbur wrote:

That makes perfect sense. But, I just get a blank page when it comes to
the ruby code. Example:

did this display?

<% for post in @posts do %>
Post Title:<%= post.title %>
Created At: <%= post.date_created %>
<% end %>

That’s in the show_search.rhtml The “did this display?” part shows up,
but nothing below it. I double checked to make sure the form action tag
is now set to show_search, and it is. Any ideas?

Check you log files in the /log folder. See if it has anything useful.

On 6/13/06, Jasbur [email protected] wrote:

That’s in the show_search.rhtml The “did this display?” part shows up,
but nothing below it. I double checked to make sure the form action tag
is now set to show_search, and it is. Any ideas?

def search
@post = Post.find_all_by_title(params[:title])
end

<% for post in @posts do %>
Post Title:<%= post.title %>
Created At: <%= post.date_created %>
<% end %>

Notice the inconsistency? I would imagine you need to change @post to
@posts in your controller method. I would think you’d be getting an
error message though… :: shrug ::

-Curtis

I made the changes so they are all listed as “@posts” still nothing
being returned.

Here’s some more info:

Log entry:

Processing PostController#show_search (for 127.0.0.1 at 2006-06-13
18:21:22) [POST]
Session ID: 031ebcaaeea8333e964deb5534f30de4
Parameters: {“title”=>“garage”, “action”=>“show_search”,
“controller”=>“post”}
e[4;36;1mPost Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM
postse[0m
e[4;35;1mPost Load (0.000000)e[0m e[0mSELECT * FROM posts WHERE
(posts.title = ‘garage’ ) e[0m
Rendering within layouts/post
Rendering post/show_search
Completed in 0.04700 (21 reqs/sec) | Rendering: 0.01600 (34%) | DB:
0.01600 (34%) | 200 OK [http://127.0.0.1/post/show_search]

search.rhtml:

<%= start_form_tag :action=> “show_search” %>

Search


    <input type=text name="title">
    <input type="submit" value="Search &#187;" class="primary" />

<%= end_form_tag %>

post_controller.rb:

def show_search
@posts = Post.find_all_by_title(params[:title])
end

show_search.rhtml

did this display?

<% for post in @posts do %>
Post Title:<%= post.title %>
Created At: <%= post.date_created %>
<% end %>

Eureka! I figured out from the log that it was trying to do an exact
match. When i searched for the whole title it worked! Now i just have to
figure out how to do partial matches. Any ideas? I’m also going to start
a new thread for this because I think this thread has gotten a bit too
long. Thanks guys.

for a partial match use:

@posts = Post.find(:all, :conditions => [ 'LOWER(title) LIKE ?',

‘%’ + params[:title].downcase + ‘%’ ])

you don’t need to use lower or downcase, but I prefer it.

Mike

Jasbur, you are into debugging technique now. You might want to toss a
breakpoint call into your controller and then take a look at @posts to
see if anything gets assigned.

cheers,
phil

Jasbur [email protected] wrote: I made the changes so they are all
listed as “@posts” still nothing
being returned.