0

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.

nulltek
  • 3,247
  • 9
  • 44
  • 94
  • You should check why request doesn't have format associated with it. If format requested is wildcard */* then rails would return output based on first `format` defined – Vijay Agrawal Aug 11 '17 at 21:30
  • https://stackoverflow.com/questions/21820754/rails-controller-processing-as – Vijay Agrawal Aug 11 '17 at 21:31
  • @VijayAgrawal I looked at this and I can't see why the regular html form does not post properly. When I looked at the answer you posted from that question I changed my dataType: 'json' in my coffeescript and the json form posts but now the html form posts as JSON for some reason: `Started POST "/facilities" for 127.0.0.1 at 2017-08-11 17:22:00 -0500 Processing by FacilitiesController#create as JSON Parameters: {"utf8"=>"✓", "authenticity_token"=>"k9yx+p+eNENcqtEGF/g7sV35qE4QH6IzDv0yJwjU9iEPbIhoL0zfpfXkYHbprljrIGuTMZcB7xt464fg==", "facility"=>{"name"=>"johnson", "acuity_modifier"=>"HH1"}}` – nulltek Aug 11 '17 at 22:22
  • 1
    I figured this out. I had two forms (one for the JSON and one for html) both had the same class name of `new_facility` so the coffeescript I wrote to listen for the dom attribute was always posting as JSON instead of using html or JSON. I manually overrode the class and id on the html form and it works as expected. – nulltek Aug 11 '17 at 22:41

0 Answers0