0

I am a rails newbie. I am trying to implement simple search in my Tweets but it displays the entire tweets that are there and not the particular tweet I am searching for. Using SQLITE and Rails 3.2.6

I looked at RailsCasts#37 and bunch of other tutorials but I dont know where I am making a mistake.

My Tweet Model

class Tweet < ActiveRecord::Base
  belongs_to :user
  attr_accessible :body
    def self.search(search)
        if search
            find(:all, :conditions => ['body LIKE ?', "%#{search}%"])
        else
            find(:all)
        end
    end

end

Controller

 @tweets = Tweet.search(params[:search])

Form in my Index page

<%= simple_form_for tweets_path, :method => 'get', :html => { :class => 'pull-right' } do |f| %>
    <%= f.input :search, :label => false, :input_html => { :class => 'span3'} %>
<% end %>

The Tweet Migration

class CreateTweets < ActiveRecord::Migration
  def change
    create_table :tweets do |t|
      t.text :body
      t.references :user

      t.timestamps
    end
    add_index :tweets, :user_id
  end
end

The URL when I search something!

http://localhost:3000/tweets?utf8=%E2%9C%93&%2Ftweets%5Bsearch%5D=awesome

Looking for help and support! Thanks in advance!

gkolan
  • 1,571
  • 2
  • 20
  • 37
  • Could be an issue with the `params` hash. You can debug the `params` hash by putting `raise params.to_yaml` in your controller's search action, to see what string is being passed. Also, you can see if it's a problem with the Tweet.search function by testing `Tweet.search("some text")` in the Rails terminal. – bevanb Aug 03 '12 at 05:02
  • thank you for the comment. I tried with Tweet.search("some text") and the search is working. Cant seem to get an idea on how to use params(:search) – gkolan Aug 03 '12 at 05:45

1 Answers1

0

I think you're getting the name of your param wrong. look here, 'The name passed to form_for controls the key used in params to access the form’s values', So in your controller you should write something like:

@tweets = Tweet.search(params[:tweets][:search])
davidrac
  • 10,723
  • 3
  • 39
  • 71
  • Thanks a lot for the comment! I replaced Tweet.search(params[:search]) with Tweet.search(params[:tweets][:search]), I am getting this error undefined method `[]' for nil:NilClass Any thoughts? – gkolan Aug 03 '12 at 05:40
  • The name of the param is probably not tweets. you can output your params and see exactly what is the name of the parameter. – davidrac Aug 03 '12 at 05:44
  • I am sorry to ask you this! How exactly to do that? – gkolan Aug 03 '12 at 05:47
  • I'm not sure what's the right way to use simple_form_for, but according to what i see here maybe you should pass in an object: https://github.com/plataformatec/simple_form/ – davidrac Aug 03 '12 at 05:49
  • in order to see the params you can put in your controller temporarily something like `render :text => params.to_yaml` – davidrac Aug 03 '12 at 05:51
  • thank you! I used the rails form for search. It solved everything. :) – gkolan Aug 06 '12 at 22:30