我正在遵循rails教程来学习它,我想学习涡轮增压.

这是我的index.html.erb视图:

<%= turbo_include_tags %>

<h1>Notes</h1>
<turbo-frame id="new_note">
<%= form_with(model: Note.new, url: notes_path) do |form| %>
    <div>
        <%= form.label :content %>
        <%= form.text_area :content %>
    </div>
    <%= form.submit "Create Note" %>
<% end %>
</turbo-frame>

<turbo-frame id="notes">
<div>
    <% @notes.each do |note| %>

        <div>
            <p><span><%= note.id %></span><%= note.content %></p>
            <small> <%= note.created_at %> </small>
            <%= link_to "Destroy", note_path(note), data: { turbo_method: :delete } %>          
        </div>
    <% end %>

    <%= link_to "New note", new_note_path %>
</div>
</turbo-frame>

My _list.html.erb模板:

<% notes.each do |note| %>
    <div>
        <p><span><%= note.id %></span><%= note.content %></p>
        <small> <%= note.created_at %> </small>
        <%= link_to "Destroy", note_path(note), data: { turbo_method: :delete } %>          
    </div>
<% end %>

这是我的控制器:

  def index
    @notes = Note.all.order(created_at: :desc)
  end
  ...
  def new
    @note = Note.new
  end

  def create
    @note = Note.new(note_params)

    if @note.save
      @notes = Note.all  # Reload all notes or fetch them as needed
      respond_to do |format|
        format.turbo_stream do
          render turbo_stream: turbo_stream.replace("notes", partial: "notes/list", locals: { notes: @notes })
        end
        format.html { redirect_to notes_url }  # Fallback in case JavaScript is disabled
      end
    else
      render :new
    end
  end
  ...

我想在单击提交按钮时创建新注释并更新列表. 我的代码中有两个问题. 第一个是验证后表格不干净.我不知道最好的方法.

第二个是刷新列表. 在我第一次点击提交按钮后,它只工作一次.如果不刷新页面,我无法创建第二个注释.另一个问题是新注释添加在列表的结尾而不是开头(因为我的列表是按照created_at值排序的).

推荐答案

你的值是turbo_stream.update而不是turbo_stream.replace,当你的值是replace时,你就会失go <turbo-frame id="notes">,这意味着它第二次不起作用:

render turbo_stream: turbo_stream.update("notes", partial: "notes/list", locals: { notes: @notes })

<%= turbo_include_tags %>是一个废弃的助手,不应该使用.


您不必再次重新渲染整个列表.仅将新创建的注释添加到列表中:

<!-- app/views/notes/index.html.erb -->

<div id="new_note">
  <%= render "form", note: Note.new %>
</div>

<div id="notes">
  <%= render @notes %>
</div>
<!-- app/views/notes/_note.html.erb -->

<div id="<%= dom_id note %>">
  <%= note.content %>
</div>
# app/controller/notes_controller.rb

def create
  @note = Note.new(note_params)

  respond_to do |format|
    if @note.save
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.update(:new_note, partial: "notes/form", locals: {note: Note.new}),
          turbo_stream.prepend(:notes, @note),
        ]
      end
      format.html { redirect_to notes_url(@note), notice: "Note was successfully created." }
    else
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.update(:new_note, partial: "notes/form", locals: {note: @note}),
        ]
      end
      format.html { render :new, status: :unprocessable_entity }
    end
  end
end

Ruby-on-rails相关问答推荐

有没有一种方法可以点击一个按钮来冒泡到父元素?

SASS,Rails 7:找不到要导入的样式表

比较 Rails 中的日期

rails s和bundle exec rails s有什么区别?

Rails Scope 返回 all 而不是 nil

如何在我的 rails 应用程序中测试 ActiveRecord::RecordNotFound?

使用 Ruby on Rails 从原始 IP 地址获取用户国家名称

使用 Devise 令牌登录,这是内置的吗?

禁用 RVM 或使用没有 RVM 安装的 Ruby?

如何在 Windows 上为 Ruby 安装 sqlite3?

Rails:仅当值存在时如何验证格式?

我如何找出为什么我不能#destroy() 记录?

Ruby gem 命名约定

Ruby on Rails 中的MySQL 服务器已经消失

如何在 ROR (Ruby) 中显示 PDF?

counter_cache 实现的问题

Rails:一次添加多个 flash[:notice] 的简单方法

Coffeescript ||= 模拟?

Rails:如何将日期时间字符串解析为特定时区

为什么 Rails 需要 JavaScript 运行时?