Danli Denny Deng
Danli Denny Deng

Reputation: 391

Why does the server process twice for show action in rails?

Started GET "/microposts/29" for 127.0.0.1 at 2018-04-27 22:08:41 +0800
Processing by MicropostsController#show as HTML

one time...

Started GET "/microposts/29" for 127.0.0.1 at 2018-04-27 22:08:41 +0800
Processing by MicropostsController#show as */*

second time.

I have created micropost model with rails g scaffold, the server is in default setting, the index, create, and edit actions are all being processed one time.

I don't know why the show action got processed twice by the server? Can anyone help me with this? (ruby 2.3.3, rails 5.0.1)

Upvotes: 2

Views: 318

Answers (1)

Anand
Anand

Reputation: 6531

Some browsers (Safari) send Accept */* which is not mapped to html format, and for some reason (alphabetic order?) maps to js format. Then protect_from_forgery kicks in and since this request doesn't have X-Requested-With (why should it? it's an ordinary get) browser comes back with a 422 response.

Technically this is not an issue with Rails but with the browser,

You can get rid of this by modifying respond_to do |format|... end block (i think in create and update action from which its been redirected to show action.) comment line for format.js or format.json

Or,In controller:

before_action do
  if request.format == Mime::ALL
    request.format = request.xhr? ? :js : :html
  end
end

Upvotes: 2

Related Questions