I realize thi may be more complex issue than can be asnwered in this post. But, what I need to do is create a simple query on my existing tables via a search form. I have half my app done (I can write all the info I want to my database), but pulling the data out dynamically is giving me trouble. I have both the "ruby for rails" book and the Agile Development book. They both give great info on static queries. Such as: post = Posts.find_all_by_title("green") But I need to be able to pull the record dynamically. For instance, a form where I can type in "green" and search the "title" and "body" columns in the "posts" table. It seems like a simple enough query but I''m having trouble finding good documentation. Any ideas on how to do this or where I might find a good tutorial for this kind of query? -- Posted via http://www.ruby-forum.com/.
Jasbur wrote:> I realize thi may be more complex issue than can be asnwered in this > post. But, what I need to do is create a simple query on my existing > tables via a search form. I have half my app done (I can write all the > info I want to my database), but pulling the data out dynamically is > giving me trouble. > > I have both the "ruby for rails" book and the Agile Development book. > They both give great info on static queries. Such as: > > post = Posts.find_all_by_title("green") > > But I need to be able to pull the record dynamically. For instance, a > form where I can type in "green" and search the "title" and "body" > columns in the "posts" table. > > It seems like a simple enough query but I''m having trouble finding good > documentation. Any ideas on how to do this or where I might find a good > tutorial for this kind of query?In your action code: post = Post.find_all_by_title(params[:title]) Your form should have a field like: <input type=text name="title"> If you want other fields involved in the search, you can use find_all_by_field_1_and_field_2 I believe. More than that, and you could do seperate finds and then intersect them. I''m not sure what the best practice is for situations like that. -- Posted via http://www.ruby-forum.com/.
Ok, that helped. But now the form seems to take the input. But when I hit the "search" button it just stays on the search page and clears out the text area. I''ll post my code below. search.rhtml: <%= 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 %> post_controller.rb: def search post = Post.find_all_by_title(params[:title]) end I realize I''m probablly missing something in the "search" action to display the results. But this is where I''m hitting a brick wall. I''m having trouble cramming one more peice of information into my head :) -- Posted via http://www.ruby-forum.com/.
> I realize I''m probablly missing something in the "search" action to > display the results. But this is where I''m hitting a brick wall. I''m > having trouble cramming one more peice of information into my head :)...yeah, that about sums it up. Add something like <%= post.value %> In the body somewhere to display the results. You''ll probably get an error if you go to the page with no search text, so check to make sure post isn''t nil before attempting to print anything about it. -- Posted via http://www.ruby-forum.com/.
I think in your search method post should really be @post ... -- G. On 6/12/06, Jasbur <jasbur@gmail.com> wrote:> Ok, that helped. But now the form seems to take the input. But when I > hit the "search" button it just stays on the search page and clears out > the text area. I''ll post my code below. > > search.rhtml: > > <%= 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 %> > > post_controller.rb: > > def search > post = Post.find_all_by_title(params[:title]) > end > > I realize I''m probablly missing something in the "search" action to > display the results. But this is where I''m hitting a brick wall. I''m > having trouble cramming one more peice of information into my head :) > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I''m looking at creating the same search he did, and I even used his code as a starting point (thanks!). However, I still can''t get this to work. It takes my search query and spits out a page that says " NoMethodError in Recipes#results" and "undefined method `value'' for []:Array". Here''s what I have so far: search.rhtml: <%= start_form_tag :action=> "search" %> <div title="Search" id="searchform" class="form"> <h3>Search</h3> <input type=text name="title"> <input type="submit" value="Search »" class="primary" /> <%= end_form_tag %> results.rhtml: <%= @recipe.value %> recipes_controller.rb: def search @recipe = Recipe.find_all_by_title(params[:title]) redirect_to :action => ''results'' end def results @recipe = Recipe.find_all_by_title(params[:title]) end Thanks! Dave Guido Sohne wrote:> I think in your search method post should really be @post ... > > -- G.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---