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 »"
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?
--
Posted via http://www.ruby-forum.com/.
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
On 6/13/06, Jasbur <jasbur@gmail.com> wrote:> 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
»" 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?
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
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? -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
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
I made the changes so they are all listed as "@posts" still nothing being returned. -- Posted via http://www.ruby-forum.com/.
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"}
[4;36;1mPost Columns (0.016000) [0;1mSHOW FIELDS FROM
posts
[4;35;1mPost 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 »"
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 %>
--
Posted via http://www.ruby-forum.com/.
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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060613/8f4737d7/attachment.html
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. -- Posted via http://www.ruby-forum.com/.
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
On 6/13/06, Jasbur <jasbur@gmail.com> wrote:> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>