Home > Mobile >  Why is a Rails Action processed as TURBO_STREAM even though I never explicitely said so? [Solved]
Why is a Rails Action processed as TURBO_STREAM even though I never explicitely said so? [Solved]

Time:07-29

I am doing a simple Rails application in Rails 7 and for some reason, the "create" form submission, which is redirecting to "show", processes that show action as TURBO_STREAM rather than HTML. I don't understand where this is coming from. Manually calling the show method (localhost:3000/books/1) works without any problem. I have tried using ViewComponents for some of the stuff I display, and I am using simple_form for the forms.

Controller:

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]

  ...(other methods omitted for brevity)...

  def show
    render(BookDetailComponent.new(book: @book))
  end

  def new
    @book = Book.new
  end

  def create
    @book = Book.new(book_params)

    if @book.save
      redirect_to book_path(@book), notice: "Book saved"
    else
      render :new, status: :unprocessable_entity
    end
  end

  private

  def set_book
    @book = Book.find(params[:id])
  end

  def book_params
    params.require(:book).permit(:title, :original_title, :sort_title, :edition, :rating, :condition, :year, :cover)
  end
end

Form:

<%= simple_form_for book do |f| %>
  ...(various form elements)...
  <%= f.button :submit %>
  <%= link_to 'Cancel', books_path, class: 'form-cancel-button' %>
<% end %>

HTML source of the generated form:

<form  id="new_book" novalidate="novalidate" action="/books" accept-charset="UTF-8" method="post">
  ...(many form elements) ...
  <input type="submit" name="commit" value="Create Book"  data-disable-with="Create Book" />
  <a  href="/books">Cancel</a>
</form>

Rails log output:

13:57:22 web.1  | Started POST "/books" for 127.0.0.1 at 2022-07-25 13:57:22  0200
13:57:22 web.1  | Processing by BooksController#create as TURBO_STREAM
13:57:22 web.1  |   Parameters: {"authenticity_token"=>"[FILTERED]", "book"=>{"title"=>"aaabaab1", "original_title"=>"", "edition"=>"", "year"=>"", "rating"=>"not_rated", "condition"=>"not_given"}, "commit"=>"Create Book"}
13:57:22 web.1  |   TRANSACTION (1.3ms)  BEGIN
13:57:22 web.1  |   ↳ app/controllers/books_controller.rb:25:in `create'
13:57:22 web.1  |   Book Create (1.8ms)  INSERT INTO `books` (`title`, `sort_title`, `year`, `created_at`, `updated_at`, `rating`, `original_title`, `condition`, `edition`) VALUES ('aaabaab1', 'aaabaab1', NULL, '2022-07-25 11:57:22.383488', '2022-07-25 11:57:22.383488', 0, '', 0, '')
13:57:22 web.1  |   ↳ app/controllers/books_controller.rb:25:in `create'
13:57:22 web.1  |   TRANSACTION (6.8ms)  COMMIT
13:57:22 web.1  |   ↳ app/controllers/books_controller.rb:25:in `create'
13:57:22 web.1  | Redirected to http://127.0.0.1:3000/books/105
13:57:22 web.1  | Completed 302 Found in 18ms (ActiveRecord: 10.0ms | Allocations: 2364)
13:57:22 web.1  | 
13:57:22 web.1  | 
13:57:22 web.1  | Started GET "/books/105" for 127.0.0.1 at 2022-07-25 13:57:22  0200
13:57:22 web.1  | Processing by BooksController#show as TURBO_STREAM
13:57:22 web.1  |   Parameters: {"id"=>"105"}
13:57:22 web.1  |   Book Load (1.5ms)  SELECT `books`.* FROM `books` WHERE `books`.`id` = 105 LIMIT 1
13:57:22 web.1  |   ↳ app/controllers/books_controller.rb:51:in `set_book'
13:57:22 web.1  |   Rendering BookDetailComponent
13:57:22 web.1  |   ActiveStorage::Attachment Load (2.0ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 105 AND `active_storage_attachments`.`record_type` = 'Book' AND `active_storage_attachments`.`name` = 'cover' LIMIT 1
13:57:22 web.1  |   ↳ app/helpers/books_helper.rb:15:in `cover_image'
13:57:22 web.1  |   Rendered BookDetailComponent (Duration: 7.7ms | Allocations: 1649)
13:57:22 web.1  | Completed 200 OK in 14ms (Views: 6.5ms | ActiveRecord: 3.5ms | Allocations: 2468)

CodePudding user response:

The problem is that the show action only renders the book details view component. Switching to a standard show.html.erb template (without viewcomponent) did the trick.

  • Related