IkegawaTaro
IkegawaTaro

Reputation: 3563

After submitting a rails form via AJAX, local server shows successful GET but browser does not redirect

I have a rails remote form that is submitting via AJAX and is successfully saving a new Transaction to the database. Additionally, the form is processing nested_attributes and inserting a has_one related Inspection into the DB successfully.

Upon submission here is what is shown in the local server log (no errors):

Started POST "/transactions" for 127.0.0.1 at 2013-11-26 10:49:49 -0800
Processing by TransactionsController#create as */*
  Parameters: {"utf8"=>"✓", "transaction"=>{"quote_id"=>"32", "customer_id"=>"27", "inspection_attributes"=>{"additional_info"=>""}}}
   (0.2ms)  BEGIN
  SQL (0.5ms)  INSERT INTO "transactions" ("created_at", "customer_id", "quote_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", Tue, 26 Nov 2013 18:49:49 UTC +00:00], ["customer_id", 27], ["quote_id", 32], ["updated_at", Tue, 26 Nov 2013 18:49:49 UTC +00:00]]
  SQL (0.4ms)  INSERT INTO "inspections" ("additional_info", "created_at", "transaction_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["additional_info", ""], ["created_at", Tue, 26 Nov 2013 18:49:49 UTC +00:00], ["transaction_id", 14], ["updated_at", Tue, 26 Nov 2013 18:49:49 UTC +00:00]]
   (2.5ms)  COMMIT
Redirected to http://0.0.0.0:3000/transactions/14
Completed 302 Found in 13ms (ActiveRecord: 3.5ms)

Started GET "/transactions/14" for 127.0.0.1 at 2013-11-26 10:49:49 -0800
    Processing by TransactionsController#show as */*
      Parameters: {"id"=>"14"}
      Transaction Load (0.7ms)  SELECT "transactions".* FROM "transactions" WHERE "transactions"."id" = $1 LIMIT 1  [["id", "14"]]
      Rendered transactions/show.html.erb within layouts/application (0.4ms)
      Rendered layouts/_shim.html.erb (0.3ms)
      Rendered layouts/_fb_javascript_sdk.html.erb (0.3ms)
      Rendered layouts/_header.html.erb (1.1ms)
      Rendered layouts/_footer.html.erb (0.4ms)
    Completed 200 OK in 72ms (Views: 69.7ms | ActiveRecord: 0.7ms)

The browser however, stays on the transactions/new page and does not refresh to /transactions/14. I would like the browser to reflect the redirect to /transactions/14.

Transactions.js.coffee:

ready = ->
    $("#inspection_form").on "submit", "form", (event) ->
        $.ajax
            type: "POST"
            url: "/transactions"
            data: $("#finish_inspection").serialize() # serializes the form's elements.

        false # avoid to execute the actual submit of the form.

$(document).ready(ready)
$(document).on('page:load', ready)

Transactions_controller.rb:

def create
    @transaction = Transaction.new(transaction_params)
    respond_to do |format|
      if @transaction.save
        format.html { redirect_to @transaction, notice: 'Transaction was successfully created.' }
        format.json { render action: 'show', status: :created, location: @transaction }
      else
        format.html { render action: 'new' }
        format.json { render json: @transaction.errors, status: :unprocessable_entity }
      end
    end
  end

Upvotes: 1

Views: 290

Answers (1)

Sahil Grover
Sahil Grover

Reputation: 1915

    format.json { render action: 'show', status: :created, location: @transaction }

This line will render the Json format as designed/formatted for the show action.

So, rather than doing thi redirect. Try using remote => true rather than writing ajax function. In controller do something like

def create
  @transaction = Transaction.new(transaction_params)
  respond_to do |format|
    if @transaction.save
      format.html { redirect_to @transaction, notice: 'Transaction was successfully created.' }
      format.js
    else
      format.html { render action: 'new' }
      format.js
    end
  end
end

and in ur create.js.erb

window.location.replace("/transactions/#{@transaction.id}")

Hope that would help you :)

Upvotes: 1

Related Questions