Encountering an issue with cross-origin requests being blocked using rack-cors 0.2.9 on Rails 4.1.0 for my API. Employing Angularjs as a front end to submit a new record to this Rails project results in the following error displayed by Chrome:
XMLHttpRequest cannot load http://localhost:3000/passages. The request
was redirected to 'http://localhost:3000/passages/67', which is
disallowed for cross-origin requests that require preflight.
The root cause of this problem seems to be related to the "create" action within the controller:
def create
@passage = Passage.new(passage_params)
respond_to do |format|
if @passage.save
format.html { redirect_to @passage, notice: 'Passage was successfully created.' }
format.json { render :show, status: :created, location: @passage }
else
format.html { render :new }
format.json { render json: @passage.errors, status: :unprocessable_entity }
end
end
end
After saving the passage, the redirection to the specific record's "show" action triggers the XHR error.
My query revolves around configuring the response in the Rails controller to prevent this error. If avoiding redirection, what alternative approach should I take?
Thank you
UPDATE: It should be noted that despite the error, the new record is successfully created.
UPDATE: After consulting the Rails 4 documentation, it appears possible to solely respond with headers to the browser. For reference, check out the rails documentation
However, I am now faced with a
POST http://localhost:3000/passages 406 (Not Acceptable)
error in the console. Further investigation into this status code suggests that my Rails app is not providing an acceptable response to my application. Any suggestions on what response would be considered acceptable?