首页 > 解决方案 > Ruby on Rails - redirect_to does not redirect, yet creates successful GET request

问题描述

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?

标签: ruby-on-rails

解决方案


推荐阅读