哪个模板文件扩展名提供对 Rails 7.1 和 Turbo-frames 的支持?

问题描述 投票:0回答:1

我似乎无法获得返回到浏览器进行 Turbo 处理的正确数据格式。

应用程序/视图/the_form.html.erb

<%= turbo_frame_tag "dog_quick_search" do %>
    <% form_tag quick_search_via_form_dogs_path(format: :turbo_stream) do %>
    blah
    blah
    <%= submit_tag('Go', class: 'btn-warning') %>
<% end %>

应用程序/控制器/dogs_controller.rb

def quick_search_via_form
    do stuff...
    render 'xxxx'
end

应用程序/视图/狗/xxxx.....

<%= turbo_frame_tag "dog_quick_search" do %>
  Bring back ujs
<% end %>

根据 xxxx 的文件扩展名,我得到:

xxxx.html.erb:

ActionView::MissingTemplate (Missing template dogs/xxxx, application/xxxx with {:locale=>[:en], :formats=>[:turbo_stream], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

xxx.erb:

The contents of xxxx.erb replace the entire webpage in the browser

xxx.turbo-stream.html.erb

ActionView::MissingTemplate (Missing template dogs/xxxx, application/xxxx with {:locale=>[:en], :formats=>[:turbo_stream], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

从阅读 Turbo 的内容来看,我所要做的就是在源和渲染模板中匹配 Turbo-frame id,并使用通常的 .html.erb 扩展名,它将用 Turbo-frame 的内容替换 Turbo-frame回。 由于我无法返回 .html.erb 模板,我想知道这是否是问题所在,但不知道如何停止有关无法找到该特定模板的错误。

更新:

通过向渲染指令添加显式格式,它可以工作:

render 'xxxx' formats: :html

老实说,我不明白这一点 - 如果模板具有扩展名 .html.erb 肯定会告诉 Rails 它是一个 html 模板,就像以前的 Rails 版本中所做的那样?

ruby-on-rails turbolinks
1个回答
0
投票

我所要做的就是拥有匹配的涡轮框架ID

这正是您需要做的,但您可能希望将表单保持在框架之外:

# app/views/the_form.html.erb

# don't add `format: :turbo_stream`
<%= form_with url: quick_search_via_form_dogs_path, method: :get,
  data: {turbo_frame: :dog_quick_search} do |f| %>

  <%= f.text_field :search %>
  <%= f.submit "Go", class: "btn-warning" %>
<% end %>

<%= turbo_frame_tag "dog_quick_search", data: {turbo_action: :advance} %>
# app/controllers/dogs_controller.rb

def quick_search_via_form
  render "search"
end
# app/views/dogs/search.html.erb

<%= turbo_frame_tag "dog_quick_search" do %>
  <%= params[:search] %>
<% end %>
© www.soinside.com 2019 - 2024. All rights reserved.