简体   繁体   中英

redirect_to inside controller action doesn't work

I use Rails 5.1 and the "redirect_to @search" inside create action doesn't work.

My SearchesController:

class SearchesController < ApplicationController
  def new
    @search = Search.new
  end

  def create
    @search = Search.create!(search_params)
    redirect_to @search # search_path(@search) doesn't work either
  end

  def show
    @search = Search.find(params[:id])
  end

private

  def search_params
    params.require(:search).permit!
  end
end

After creating a new search entry by clicking the submit button it doesn't redirect to show page.

My app/views/searches/new.html.erb:

<div>
  <h1>Advanced Search Form</h1>
    <%= form_with model: @search do |form| %>
      <%= form.text_field :keywords %>
      <%= form.select :ort, options_from_collection_for_select(Imagecapturing.cities, :ort, :city_name, prompt: false, include_blank: false) %>
      <%= form.submit("Suchen", :id=>"button", :class=>"Test", :name=>"submit") %>
    <% end %>
</div>

config/routes.rb:

Rails.application.routes.draw do
  root 'imagecapturings#index'
  resources :searches
end

Log:

Started POST "/searches" for ::1 at 2018-06-14 17:37:54 +0200
Processing by SearchesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"B91lIUxZZvanOx1luhhfWBJ9mAO5Np/6Bx4xzPdv2Ygj29bprWk5+wIBP7kMVl5Eoxz0KcyJF5DK8UaVUhQaFQ==", "search"=>{"keywords"=>"", "ort"=>"A-St. Paul"}, "submit"=>"Suchen"}
   (0.6ms)  BEGIN
  SQL (22.8ms)  INSERT INTO `searches` (`keywords`, `ort`, `created_at`, `updated_at`) VALUES ('', 'A-St. Paul', '2018-06-14 15:37:58', '2018-06-14 15:37:58')
   (11.1ms)  COMMIT
Redirected to http://localhost:4000/searches/23
Completed 302 Found in 50ms (ActiveRecord: 34.5ms)


Started GET "/searches/23" for ::1 at 2018-06-14 17:37:58 +0200
Processing by SearchesController#show as JS
  Parameters: {"id"=>"23"}
  Search Load (0.7ms)  SELECT  `searches`.* FROM `searches` WHERE `searches`.`id` = 23 LIMIT 1
  Rendering searches/show.html.erb within layouts/application
  Imagecapturing Load (10.4ms)  SELECT `imagecapturing`.* FROM `imagecapturing` WHERE (ort LIKE '%A-St. Paul%') ORDER BY `imagecapturing`.`id` DESC
  Rendered searches/show.html.erb within layouts/application (62.5ms)
  Rendered layouts/_top_nav.html.erb (6.0ms)
  Imagecapturing Load (17.0ms)  SELECT distinct(ort) FROM `imagecapturing` ORDER BY `imagecapturing`.`ort` ASC
  Rendered searches/_links.html.erb (33.5ms)
Completed 200 OK in 409ms (Views: 340.1ms | ActiveRecord: 28.0ms)

Is in "Processing by SearchesController#show as JS" the "as JS" part the issue?

How can I get the redirect working so that after clicking the submit button it redirects to the show action?

--- UPDATE

With suggestion of user Rockwell I modified the create action to:

def create
    @search= Search.create!(search_params)
    respond_to do |format|
      if @search
        format.html {redirect_to @search}
        format.json { render :show, status: :created, location: @step }
        format.js { redirect_to @search }
      else
        format.html { render :new }
        format.json { render json: @search.errors, status: :unprocessable_entity }
        format.js { render :new }
      end
    end
  end

But it still processes "as JS":

Started POST "/searches" for ::1 at 2018-06-14 18:09:18 +0200
Processing by SearchesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"D7l5ibv8hu08Z18VKqa0Y+iqGHfT+SIZTIoM8vHdcpArv8pBWszZ4Jldfcmc6LV/Wct0XaZGqnOBZXurVKaxDQ==", "search"=>{"keywords"=>"", "ort"=>"Cologny"}, "submit"=>"Suchen"}
   (0.2ms)  BEGIN
  SQL (11.8ms)  INSERT INTO `searches` (`keywords`, `ort`, `created_at`, `updated_at`) VALUES ('', 'Cologny', '2018-06-14 16:09:18', '2018-06-14 16:09:18')
   (17.0ms)  COMMIT
Redirected to http://localhost:4000/searches/30
Completed 302 Found in 36ms (ActiveRecord: 29.0ms)


Started GET "/searches/30" for ::1 at 2018-06-14 18:09:19 +0200
Processing by SearchesController#show as JS
  Parameters: {"id"=>"30"}
  Search Load (0.8ms)  SELECT  `searches`.* FROM `searches` WHERE `searches`.`id` = 30 LIMIT 1
  Rendering searches/show.html.erb within layouts/application
  Imagecapturing Load (28.1ms)  SELECT `imagecapturing`.* FROM `imagecapturing` WHERE (ort LIKE '%Cologny%') ORDER BY `imagecapturing`.`id` DESC
  Rendered searches/show.html.erb within layouts/application (16157.3ms)
  Rendered layouts/_top_nav.html.erb (3.6ms)
  Imagecapturing Load (27.6ms)  SELECT distinct(ort) FROM `imagecapturing` ORDER BY `imagecapturing`.`ort` ASC
  Rendered searches/_links.html.erb (39.2ms)
Completed 200 OK in 16404ms (Views: 16314.1ms | ActiveRecord: 56.5ms)

I believe that you are correct, the Processing by SearchesController#show as JS is what is causing your error. If you respond to the format it should solve your issue. I added HTML and JSON here but you wouldn't need to if you know you will never need that, just the js should be fine.

def create
  @search = Search.new(search_params)

  respond_to do |format|
    if @search.save
      format.html {redirect_to @search}
      format.json { render :show, status: :created, location: @step }
      format.js { redirect_to @search }
    else
      format.html { render :new }
      format.json { render json: @search.errors, status: :unprocessable_entity }
      format.js { render :new }
    end
  end
end

If you are sending this as an AJAX you could specify the type that gets sent as well, but I would need to see the JS code for that to give a solution to fix that.

it is working as expected, you can see it on the log, the insert is done and then the redirect too.

but the problem is the format you are asking. it seems like the create is called as js Processing by SearchesController#create as JS (maybe you are doing an ajax request with that format or your form has remote:true). and then the redirect isn't done visually of course. you need to make the create request without js format or don't make the redirect on the controller and create a "create.js" view that does the redirect. but of course it's not the best solution.

if you want to do the redirect, the best way is to change the format of the create request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM