Rails 中删除 turbolinks 后如何修复控制器重定向
How to fix controller redirect after removing turbolinks in Rails
我已经使用 Ruby 2.6.1 从我的 Rails 6.0.0 beta web 应用程序中删除了 turbolinks,并且在这样做之后,每当我更新或创建章节时,POST 请求将被发送到数据库。但是数据库在保存数据的同时,也会尝试重定向页面,但是浏览器不会重定向到任何地方,仍然会显示 edit/new 页面。
在删除 turbolinks 之前这不是问题。我试过四处搜索,但到目前为止找不到任何解决方案。我尝试重新启动服务器,当然事先使用 bundle update
实际 删除 turbolinks。
章节控制器
def update
@book = Book.find(params[:book_id])
@chapter = @book.chapters.find(params[:id])
if @chapter.update(chapter_params)
redirect_to book_chapter_path(@book, @chapter)
else
...
end
end
...
private
def chapter_params
params.require(:chapter).permit(:title, :content, :status)
end
章节 > edit.html.erb
<%= form_with model: @chapter, url: { action: "update" } do |form| %>
...
<%= form.submit "Draft", class: "save-btn" %>
...
<% end %>
routes.rb
resources :books do
resources :chapters
end
通常我希望在 POST 请求 update/create 数据库中的章节后服务器日志中有此输出...
Started GET "/books/1/chapters/1" for 127.0.0.1 at 2019-02-17 12:52:29 +1030
Processing by ChaptersController#show as HTML
但相反,它说
Started GET "/books/1/chapters/1" for 127.0.0.1 at 2019-02-17 12:52:29 +1030
Processing by ChaptersController#show as JS
我想我明白为什么会这样,但同样,我不知道如何解决它。
form_with
的默认值是 Ajax(远程)。
https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with
您应该添加 local: true
.
<%= form_with model: @chapter, url: { action: "update" }, local: true do |form| %>
我已经使用 Ruby 2.6.1 从我的 Rails 6.0.0 beta web 应用程序中删除了 turbolinks,并且在这样做之后,每当我更新或创建章节时,POST 请求将被发送到数据库。但是数据库在保存数据的同时,也会尝试重定向页面,但是浏览器不会重定向到任何地方,仍然会显示 edit/new 页面。
在删除 turbolinks 之前这不是问题。我试过四处搜索,但到目前为止找不到任何解决方案。我尝试重新启动服务器,当然事先使用 bundle update
实际 删除 turbolinks。
章节控制器
def update
@book = Book.find(params[:book_id])
@chapter = @book.chapters.find(params[:id])
if @chapter.update(chapter_params)
redirect_to book_chapter_path(@book, @chapter)
else
...
end
end
...
private
def chapter_params
params.require(:chapter).permit(:title, :content, :status)
end
章节 > edit.html.erb
<%= form_with model: @chapter, url: { action: "update" } do |form| %>
...
<%= form.submit "Draft", class: "save-btn" %>
...
<% end %>
routes.rb
resources :books do
resources :chapters
end
通常我希望在 POST 请求 update/create 数据库中的章节后服务器日志中有此输出...
Started GET "/books/1/chapters/1" for 127.0.0.1 at 2019-02-17 12:52:29 +1030
Processing by ChaptersController#show as HTML
但相反,它说
Started GET "/books/1/chapters/1" for 127.0.0.1 at 2019-02-17 12:52:29 +1030
Processing by ChaptersController#show as JS
我想我明白为什么会这样,但同样,我不知道如何解决它。
form_with
的默认值是 Ajax(远程)。
https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with
您应该添加 local: true
.
<%= form_with model: @chapter, url: { action: "update" }, local: true do |form| %>