Rails render wrong format type -


i have problem rails respond_with, renders json instead of html when access through browser. problem occurs not, when load time high.

here's specification

  1. in controller, use respond_to :html, :json
  2. in last statement of action, use respond_with(@object)
  3. i use same endpoint (i.e /reports, not /reports nor /reports.json) both browser (html) , mobile (json), json request done attaching http header accept: application/json. works on json request, html request render json instead of html
  4. ruby v2.3.1 , rails v4.2.6
  5. only happens on production nginx server, never happens on development
  6. production server: unicorn v5.1.0
  7. i use own responder json key instead of formatted error message when record isn't valid, in applicationcontroller put self.responder = apiresponder

this code of apiresponder:

class apiresponder < actioncontroller::responder   def json_resource_errors     i18n.locale = "json"     resource.valid? # refreshes locale in error messages translated     { errors: resource.errors }   end    protected      def api_behavior       raise missingrenderer.new(format) unless has_renderer?        if get?         display resource       elsif post?         display resource, :status => :created, :location => api_location       elsif patch? or put?         display resource, :status => :ok, :location => api_location       else         head :no_content       end     end end 

this log rails request when wrong render json instead of html:

i, [2017-07-25t02:50:02.594851 #23977]  info -- : started "/reports" x.x.x.x @ 2017-07-25 02:50:02 +0000 i, [2017-07-25t02:50:02.596151 #23977]  info -- : processing reportscontroller#index html i, [2017-07-25t02:50:03.237783 #23977]  info -- :   rendered regions/_region.json.jbuilder (0.4ms) i, [2017-07-25t02:50:03.238063 #23977]  info -- :   rendered sales/_sales.json.jbuilder (1.0ms) ...truncated... i, [2017-07-25t02:50:03.284629 #23977]  info -- :   rendered reports/index.json.jbuilder (61.7ms) i, [2017-07-25t02:50:03.285204 #23977]  info -- : completed 200 ok in 689ms (views: 56.0ms | activerecord: 68.7ms) 

as see, rails see request html have no idea why renders json.

and here's log when renders html normally:

i, [2017-07-25t02:52:05.132039 #8991]  info -- : started "/reports" x.x.x.x @ 2017-07-25 02:52:05 +0000 i, [2017-07-25t02:52:05.187351 #8991]  info -- : processing reportscontroller#index html i, [2017-07-25t02:52:06.115379 #8991]  info -- :   rendered shared/_filter_date.html.erb (1.0ms) i, [2017-07-25t02:52:06.161682 #8991]  info -- :   rendered reports/_reports.html.erb (45.6ms) i, [2017-07-25t02:52:06.163072 #8991]  info -- :   rendered reports/index.html.erb within layouts/application (51.2ms) i, [2017-07-25t02:52:06.181696 #8991]  info -- : completed 200 ok in 994ms (views: 66.3ms | activerecord: 84.9ms) 

both request above accessed browser, former renders json (should html).

i wonder if related server or production configuration , production, because never happens on development.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -