Reputation: 6761
I am trying to understand the mechanics of asynchronously updating Rails views by rendering JSON (as my boss wants it done that way).
Not very successful so far.
Is there a way to inspect the response as it is seen on the OS layer? Like when I use curl
? I am trying to write up my learnings for a possible blog post about this topic and having a way to visualize what Rails sends out when rendering JSON would be really helpful.
The important controller part, looks like this:
def create
@order = Order.find_by(id: params[:order_id])
@comment = current_user.comments.new(comment_params)
.
.
return unless @comment.save!
respond_to do |format|
format.json { render json: @comment, context: self }
end
end
EDIT:
Based on one of the comments I tried inspecting the response with the debugger/pry, first making sure the @comment object contains data:
(byebug) @comment
#<Comment id: 3090, order_id: 125, user_id: 18, content: "asdfad", created_at: "2017-02-01 12:21:25", updated_at: "2017-02-01 12:21:25">
Seems good.
(byebug) response.body
""
Not so cool, where is the JSON data? Why is the body empty?
Upvotes: 3
Views: 3612
Reputation: 620
The approach described above using a spec is totally valid and good. Also you then already have specs in place :)
Anyway one may face some other valid use-cases (there maybe more than the once below) where one needs to inspect requests / responses.
1. perform some request with javascript to a local / current application
One can use e.g. the chrome web dev tools to inspect the request, response and headers. This is very useful and handy.
Also you can tail your application log and see the incoming request with params. In rails development you will also see a lot of rails debug information such as active record statements, render calls, mailer interaction and so on.
In addition to the information provided you can add as much debug output (assuming you are in rails development mode) with
logger.debug "any important information you need"
or even debug with breakpoints [1].
2. perform some request with javascript to a remote / other application
In case you have control over the remote application then this is basically them as 1. You just tail the log of the other application. :)
3. perform a request from within a rails controller to another application
In case you have control over the other application you can again tail its log, debug with breakpoints or add log output (e.g. from the @request
variable which is available in controller level)
Also you can use debug logging in the main application and output the response received or debug right in that spot.
If your are using some http client as faraday [2] (which I can highly recommend) you can active its logging. See the basic use chapter [3]. With faraday you can even provide your own logger or use some 3rd party logger [4] to get nicer outputs.
[1] http://guides.rubyonrails.org/debugging_rails_applications.html
[2] https://github.com/lostisland/faraday
[3] https://github.com/lostisland/faraday#basic-use
[4] https://github.com/envylabs/faraday-detailed_logger
Upvotes: 2
Reputation: 818
You can write a test to achieve your goal or you can put the code after_action {puts response.body }
to a controller.
application_controller.rb
class ApplicationController < ActionController::Base
...
after_action { puts response.body }
...
end
some_test_spec.rb
require 'rails_helper'
RSpec.describe YourController, type: :controller do
describe "GET #index" do
it "returns some data" do
get :index
puts response.body
binding.pry # for interactive debugging
expect(response.status).to eq(200)
end
end
end
Upvotes: 6