ruby-on-rails 哪个模板文件扩展名支持Rails 7.1和turbo-frames?

sczxawaw  于 5个月前  发布在  Ruby
关注(0)|答案(2)|浏览(61)

我似乎无法获得正确格式的数据返回到浏览器进行涡轮处理。
app/views/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 %>

字符串
app/controllers/dogs_controller.rb

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


app/views/dogs/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。由于我无法返回.html.erb模板,我想知道这是否是问题所在,但无法看到如何阻止无法找到该特定模板的错误。

更新:

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

render 'xxxx' formats: :html


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

q3qa4bjr

q3qa4bjr1#

我只需要找到匹配的涡轮帧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 %>
z9ju0rcb

z9ju0rcb2#

解决了
问题被确定为表单提交未被Turbo拦截。
这是因为需要通过运行

$ bin/importmap pin bootstrap

字符串
现在表单以TURBO-STREAM的形式提交。标签内容不会被替换,但这是明天的问题!

相关问题