I have a Rails 5.1.2 application that has a model called Facility. In another form I create Facility inside of the form using JSON via an ajax call and that works fine.
I have another view which is the typical https//:lvh.me:3000/facilities/new which is a standard rails HTML form.
When I submit the rails HTML form it gets submitted as */*
in the request which returns json and never redirects.
Here is my simple controller code:
def create
@facility = Facility.new(facility_params)
respond_to do |format|
if @facility.save
format.json {render json: @facility}
format.html {redirect_to facilities_path, notice: "#{@facility.name} has been created"}
else
format.json {render json: {errors: @facility.errors.full_messages}}
format.html {render :new}
end
end
end
What's weird is if I take the format and switch html as the first and json as the second, the html form will start working and the ajax call in the other form does not work and yields an html response instead of a JSON response.
Am I doing something so simple and wrong that it's ridiculous or is there a potential bug in Rails?
I ask because I've been messing around with this for 2 hours and haven't found anything very useful on Google/Stack.
This is not a duplicate question, the link provided in the comments does not apply to this use case. In this case the json format is taking priority and the HTML doesn't yield a HTML response even though it has it in the respond_to block. If I switch the order of response to html first then json second the html form starts working but the json/ajax doesn't work properly and yields and html response.