Rails Console tips
Sometimes you want to test queries etc from the Rails Console that requiers you to be logged in (for example via Devise).
This is how you do it:
credit to Eric London: https://stackoverflow.com/questions/4929078/how-to-sign-in-a-user-using-devise-from-a-rails-console
(note that the accepted answer is not the best one!)
Code is modified sine I use virtual attribute “login” (username or email) instead of just email
# get csrf token app.get '/users/sign_in' csrf_token = app.session[:_csrf_token]
# log in app.post('/users/sign_in', {"authenticity_token"=>csrf_token, "user"=>{"login"=>"fshsweden", "password"=>"*******"}})
# get new csrf token, as auth user app.get '' csrf_token = app.session[:_csrf_token]
# make a POST request app.post '/some_request.json', {"some_value"=>"wee", "authenticity_token"=>csrf_token}
# make a GET request app.get '/some_other_request.json'
Accessing the current user
app.controller.current_user
Benchmarking code
puts Benchmark.measure { User.find(:first, :select => :id, :conditions => ["name = ?","Joe"]).id }
Faking Web requests
app.post('/foo', {"this" => "that", "items" => ["bar", "baz"]}) app.put('/foo', {"this" => "that", "items" => ["bar", "baz"]})