I am trying to implement searching in my rails app. I have a User model where each user has an id and a facebook_id.
How can I implement searching by facebook_id? I was looking to call something like this: localhost:3000/users.json?facebook_id=123456.
Thanks!
Edit: I tried a conditional index, but received an error "We're sorry, but something went wrong."
This was my code:
def index
if params[:facebook_id]
respond_with User.find(params:facebook_id)
elsif params[:twitter_id]
respond_with User.find(params:twitter_id)
else
respond_with User.all
end
end
Rails log:
2012-08-07T19:43:11+00:00 app[web.1]: Started GET "/users.json?facebook_id=1" for 173.247.200.7 at 2012-08-07 19:43:11 +0000
2012-08-07T19:43:11+00:00 app[web.1]: Processing by UsersController#index as JSON
2012-08-07T19:43:11+00:00 app[web.1]: Parameters: {"facebook_id"=>"1"}
2012-08-07T19:43:11+00:00 app[web.1]: Completed 500 Internal Server Error in 2ms
2012-08-07T19:43:11+00:00 app[web.1]:
2012-08-07T19:43:11+00:00 app[web.1]: NameError (undefined local variable or method `facebook_id' for #<UsersController:0x000000044b6e38>):
2012-08-07T19:43:11+00:00 app[web.1]: app/controllers/users_controller.rb:4:in `index'
Edit2: After changing the params from params:facebook_id to params[:facebook_id] I received another error. Here is a copy of my rails log.
2012-08-07T19:53:45+00:00 app[web.1]: Started GET "/users.json?facebook_id=1000" for 173.247.200.7 at 2012-08-07 19:53:45 +0000
2012-08-07T19:53:45+00:00 app[web.1]: Processing by UsersController#index as JSON
2012-08-07T19:53:45+00:00 app[web.1]: Parameters: {"facebook_id"=>"1000"}
2012-08-07T19:53:45+00:00 app[web.1]: Completed 500 Internal Server Error in 2ms
2012-08-07T19:53:45+00:00 app[web.1]:
2012-08-07T19:53:45+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=1000):
2012-08-07T19:53:45+00:00 app[web.1]: app/controllers/users_controller.rb:4:in `index'