Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Pass Parameters Via URL
    primarykey
    data
    text
    <p>I am trying to implement searching in my rails app. I have a <code>User</code> model where each user has an <code>id</code> and a <code>facebook_id</code>.</p> <p>How can I implement searching by <code>facebook_id</code>? I was looking to call something like this: <code>localhost:3000/users.json?facebook_id=123456</code>.</p> <p>Thanks!</p> <p><strong>Edit</strong>: I tried a conditional index, but received an error "We're sorry, but something went wrong."</p> <p>This was my code:</p> <pre><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 </code></pre> <p>Rails log:</p> <pre><code>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"=&gt;"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 #&lt;UsersController:0x000000044b6e38&gt;): 2012-08-07T19:43:11+00:00 app[web.1]: app/controllers/users_controller.rb:4:in `index' </code></pre> <p><strong>Edit2</strong>: After changing the params from <code>params:facebook_id</code> to <code>params[:facebook_id]</code> I received another error. Here is a copy of my rails log.</p> <pre><code>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"=&gt;"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' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload