Tom Rossi
Tom Rossi

Reputation: 12066

ActionController::InvalidCrossOriginRequest in Controller Test

When running legacy controller tests like this one:

    get :edit, id: object.id, format: :js

My tests began failing in Rails 4.1 with the following error:

ActionController::InvalidCrossOriginRequest: Security warning: an embedded <script> tag on another site requested protected JavaScript. If you know what you're doing, go ahead and disable forgery protection on this action to permit cross-origin JavaScript embedding.

Upvotes: 34

Views: 10474

Answers (2)

John Pollard
John Pollard

Reputation: 3899

For Rails 5+

get :edit, params: { id: object.id }, xhr: true

Upvotes: 52

Tom Rossi
Tom Rossi

Reputation: 12066

Older versions of Rails accepted this, but the solution was to use the xhr method as follows:

  xhr :get, :edit, id: object.id

Upvotes: 41

Related Questions