RM64
RM64

Reputation: 69

How to fix controller redirect after removing turbolinks in Rails

I've removed turbolinks from my Rails 6.0.0 beta web application using Ruby 2.6.1 and upon doing so, whenever I update or create a chapter, a POST request will get sent to the database. But while the database will save the data, it will also try to redirect the page, but the browser does not redirect to anywhere and will still display the edit/new page.

This was not an issue before removing turbolinks. I have tried searching around but could not find any solution to this so far. I tried restarting the server and of course using bundle update beforehand to actually remove turbolinks.

Chapter Controller

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

chapters > 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

Normally I expect this output from the server logs after it's POST request to update/create the chapter in the database...

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

But instead, it says

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

I think I understand why this is happening, but again, I don't know how to solve it.

Upvotes: 1

Views: 880

Answers (1)

mogbee
mogbee

Reputation: 208

Default of form_with is Ajax(remote).

https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with

You should add local: true.

<%= form_with model: @chapter, url: { action: "update" }, local: true do |form| %>

Upvotes: 5

Related Questions