Forum: Ruby on Rails Find DB items by "title"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jasbur (Guest)
on 2006-06-13 22:48
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" %>

<div title="Search" id="searchform" class="form">
    <h3>Search</h3>
    <%= error_messages_for 'post' %><br/>

        <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?
D0802a5ae80c2a40a9ef07967c6dc362?d=identicon&s=25 Mike G. (random52k)
on 2006-06-13 23:26
(Received via mailing list)
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 %>
<strong>Post Title:</strong><%= post.title %>
<strong>Created At: </strong><%= post.created_at %>
<% end %>

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

Mike
Jasbur (Guest)
on 2006-06-14 00:26
That makes perfect sense. But, I just get a blank page when it comes to
the ruby code. Example:

<p>did this display?</p>

<% for post in @posts do %>
<strong>Post Title:</strong><%= post.title %>
<strong>Created At: </strong><%= 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?
Alex Wayne (Guest)
on 2006-06-14 00:31
Jasbur wrote:
> That makes perfect sense. But, I just get a blank page when it comes to
> the ruby code. Example:
>
> <p>did this display?</p>
>
> <% for post in @posts do %>
> <strong>Post Title:</strong><%= post.title %>
> <strong>Created At: </strong><%= 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.
91577d00348e23566f04e26438ace241?d=identicon&s=25 Curtis S. (cuspendlove)
on 2006-06-14 00:34
(Received via mailing list)
On 6/13/06, Jasbur <jasbur@gmail.com> 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 %>
<strong>Post Title:</strong><%= post.title %>
<strong>Created At: </strong><%= 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
Jasbur (Guest)
on 2006-06-14 01:19
I made the changes so they are all listed as "@posts" still nothing
being returned.
Jasbur (Guest)
on 2006-06-14 01:23
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"}
  Post Columns (0.016000)   SHOW FIELDS FROM
posts
  Post Load (0.000000)   SELECT * FROM posts WHERE
(posts.`title` = 'garage' ) 
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" %>

<div title="Search" id="searchform" class="form">
    <h3>Search</h3>
    <br/>

        <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

<p>did this display?</p>

<% for post in @posts do %>
<strong>Post Title:</strong><%= post.title %>
<strong>Created At: </strong><%= post.date_created %>
<% end %>
Phillip Kast (Guest)
on 2006-06-14 01:29
(Received via mailing list)
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 <jasbur@gmail.com> wrote: I made the changes so they are all
listed as "@posts" still nothing
being returned.
Jasbur (Guest)
on 2006-06-14 01:29
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.
D0802a5ae80c2a40a9ef07967c6dc362?d=identicon&s=25 Mike G. (random52k)
on 2006-06-14 02:27
(Received via mailing list)
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
This topic is locked and can not be replied to.