Hi, I want to allow users ti search my blog. I have the following code below to initiate the form. <%= start_form_tag :action => ''search'', :search => search %> <p align =center><%= text_field ''search'', ''search'',"size" =>"20" %> <%= submit_tag ''Search'' %></p> <%= end_form_tag %> And below i have the def in the blog controller def search @posts = Post.search_posts(:search) @categories = Category.find(:all, :order => "name asc") @archive = Post.month_posts(4) end I cant seem to pass the value in the search textbox to the def search in the blog controller. The SQL query on the post model works fine when i input arguments myself. Code for post model below. def self.search_posts(args) find(:all, :conditions => "title LIKE ''%{args}%'' OR body LIKE ''%{args}%''", :order => "created_at desc") end Can anyone tell me where i am going wrong? I keep getting 0 search results. I think the problem is passing the value from the form to the controller. -- Posted via http://www.ruby-forum.com/.
Use the params hash to retrieve parameter values: @posts = Post.search_posts(params[:search]) HTH Juanjo On 4/5/06, John Butler <JohnnyButler7@gmail.com> wrote:> Hi, > > I want to allow users ti search my blog. I have the following code > below to initiate the form. > > <%= start_form_tag :action => ''search'', :search => search %> > <p align =center><%= text_field ''search'', ''search'',"size" =>"20" %> > <%= submit_tag ''Search'' %></p> > <%= end_form_tag %> > > And below i have the def in the blog controller > > def search > @posts = Post.search_posts(:search) > @categories = Category.find(:all, :order => "name asc") > @archive = Post.month_posts(4) > end > > I cant seem to pass the value in the search textbox to the def search in > the blog controller. The SQL query on the post model works fine when i > input arguments myself. Code for post model below. > > def self.search_posts(args) > find(:all, > :conditions => "title LIKE ''%{args}%'' OR body LIKE ''%{args}%''", > :order => "created_at desc") > end > > Can anyone tell me where i am going wrong? I keep getting 0 search > results. I think the problem is passing the value from the form to the > controller. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I have just done that but i am still getting no search results. <%= start_form_tag :action => ''search'', :args => search %> <p align =center><%= text_field ''search'' , ''search'', "size" =>"20" %> <%= submit_tag ''Search'' %></p> <%= end_form_tag %> blog controller def search @posts = Post.search_posts(params[:args]) @categories = Category.find(:all, :order => "name asc") @archive = Post.month_posts(4) end post model def self.search_posts(args) find(:all, :conditions => "title LIKE ''%{args}%'' OR body LIKE ''%{args}%''", :order => "created_at desc") end I am using eclipse with the rad rails plug-in, is there anyway to step through the code like so i can see the value of what is being passed? -- Posted via http://www.ruby-forum.com/.
> I have just done that but i am still getting no search results.<...>> @posts = Post.search_posts(params[:args])<...> Can you spot what is different from what Juanjo suggested: @posts = Post.search_posts(params[:search]) ;) Regards, Rimantas -- http://rimantas.com/
> > Can you spot what is different from what Juanjo suggested: > > @posts = Post.search_posts(params[:search])I''ve changed the :search to :args in the controller aswell as the view so it shouldn''t make any difference should it? :search is just the name of the paramater i am passing. i changed it back to :search and it still didn''t work. 0 search results. -- Posted via http://www.ruby-forum.com/.
<..>> > I''ve changed the :search to :args in the controller aswell as the view > so it shouldn''t make any difference should it? :search is just the name > of the paramater i am passing.Ok. You use text_field, which expects model name, so you justs put search here. In this case you can access its value with params[:search][:search] So you''d have <%= text_field ''search'', ''search'',"size" =>"20" %> in form and @posts = Post.search_posts(params[:search][:search]) in controller Another possible way is to use text_field_tag insted of text_field. In this case params[:search] is OK. Then you have: In form: <%= text_field_tag ''search'',nil, :size => 20 %> (you might want to replace nil with params[:search]) and @posts = Post.search_posts(params[:search]) - controller Regards, Rimantas -- http://rimantas.com/
:search doesn''t work it must the paramter must have to be called something different than the def and the field i''m passing. The problem was in the model. I forgot to put the # before the args! :conditions => "title LIKE ''%#{args}%'' OR body LIKE ''%#{args}%''", -- Posted via http://www.ruby-forum.com/.
Thanks for your help! -- Posted via http://www.ruby-forum.com/.
John Butler wrote:> <%= start_form_tag :action => ''search'', :search => search %>The :search => search thing isn''t relevant.> <%= text_field ''search'', ''search'',"size" =>"20" %>The two args here ''foo'' and ''bar'' need accessed in the controller as params[:foo][:bar] so you''ll need [:search][:search] The first arg is for grouping. e.g you could have ''search'', ''text'' ''search'', ''category'' that would generate a params hash like this: {search => { text => ''...'', :category => ''...''} } You could then get the whole thing by params[:search] or a particular sub-part like paramns[:search][:category] HTH, Alan -- Posted via http://www.ruby-forum.com/.