ruby-on-rails 为什么一个Rails操作被当作TURBO_STREAM来处理,尽管我从来没有明确说过?

von4xj4u  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(155)

我正在Rails 7中做一个简单的Rails应用程序,由于某种原因,“create”表单提交(重定向到“show”)将操作显示为TURBO_STREAM而不是HTML。我不明白这是从哪里来的。手动调用show方法(localhost:3000/books/1)没有任何问题。我已经尝试使用ViewComponents来显示一些内容,我正在使用simple_form来显示表单。
控制器:

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

表格:

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

生成的表单的HTML源代码:

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

Rails日志输出:

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)
dfuffjeb

dfuffjeb1#

问题是show操作只呈现图书细节视图组件,切换到标准的show.html.erb模板(没有viewcomponent)就解决了这个问题。

相关问题