2

Rails 6.1.2.1 Ruby 2.7.2p137

Whenever I use the redirect_to helper in my rails app, I am not redirected to a new page. However a successful GET request is made in the browser to the correct path and the response to that request contains the markup for that page. This is very confusing to me, as I'd normally expect that to be all there is to it. I have tried this in Firefox and Chrome, with an without addons.

To further clarify, the rails server is responding correctly with a redirect to the right page, but the browser does not show that page.

Here is an example controller action that includes the redirect.

  def create
    properties =
      allowed_location_params.merge(universe_id: params[:universe_id])
    @location = Location.create!(properties)
    flash[:success] = "Location created!"
    redirect_to location_url(@location)
  end

Here is the form that invokes the above action.

<h1>New Location</h1>

<%= form_with model: @location, url: universe_locations_url(universe_id: @location.universe_id) do |form| %>
  <div class="mb-3">
    <%= form.label :name, class: "form-label" %>
    <%= form.text_field :name, class: "form-control" %>
  </div>
  <div class="mb-3">
    <%= form.label :description, class: "form-label" %>
    <%= form.text_area :description, size: "60x10", class: "form-control" %>
  </div>
  <%= form.submit "Save", class: "btn btn-primary" %>
<% end %>

It is my assumption that upon submitting the form here, I should be redirected to the show view for the new Location I just made. Instead I get a GET request in the networking tab, it returns with status 200, the response includes the correct page, and then nothing. The user is not redirected to that page.

Any idea what I'm doing wrong?

devleo
  • 483
  • 6
  • 13
  • Check your console output for the start of the submission of the form. It should tell you how the request is made (ie. HTTP, JS, AJAX). I'm guessing you might need a `local: true` in that form_with line. – sam Feb 12 '21 at 01:29
  • Also, I'd suggest switching those urls to paths in the redirects (ie. `redirect_to location_path(@location)`) – sam Feb 12 '21 at 01:30
  • That did it. `local: true` with the helper worked. When I looked up a way to make that the default, it led to [here](https://stackoverflow.com/questions/47822826/set-local-true-as-default-for-form-with-in-rails-5/49492613#49492613) with an answer that also helped me. Thank you! – devleo Feb 12 '21 at 05:15

0 Answers0