我使用Rails 5.1和" redirect_to @ search"内部创造行动不起作用。
我的搜索控制器:
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
点击提交按钮创建新的搜索条目后,它不会重定向到显示页面。
我的app / views / searching / 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>
配置/ routes.rb中:
Rails.application.routes.draw do
root 'imagecapturings#index'
resources :searches
end
日志:
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)
在&#34;由SearchesController处理#show为JS&#34; &#34;作为JS&#34;问题的一部分?
如何让重定向工作,以便在点击提交按钮后重定向到show动作?
---更新
根据用户罗克韦尔的建议,我将创建动作修改为:
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
但它仍然处理&#34;作为JS&#34;:
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)
答案 0 :(得分:1)
我相信你是对的,Processing by SearchesController#show as JS
是造成你错误的原因。如果您回复格式,它应该解决您的问题。我在这里添加了HTML和JSON,但是如果你知道你永远不需要它,你就不需要了,只需要js就可以了。
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
如果您将此作为AJAX发送,您可以指定发送的类型,但我需要查看JS代码以提供解决方案来修复它。
答案 1 :(得分:0)
它按预期工作,您可以在日志中看到它,插入完成,然后重定向。
但问题是你问的格式。似乎创建被称为js id
(也许您正在使用该格式执行ajax请求,或者您的表单具有remote:true)。然后重定向当然不是视觉上完成的。您需要在没有js格式的情况下创建创建请求,或者不要在控制器上进行重定向并创建&#34; create.js&#34;执行重定向的视图。但当然它不是最好的解决方案。
如果要进行重定向,最好的方法是更改创建请求的格式。