ruby 需要帮助了解rails 7中的turbo

7bsow1i6  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(163)

周围的一切都很奇怪。我可以创建todo对象,但它们只在我刷新后显示,我在2个不同的场合遇到过同样的问题。我知道我用错了变量,但有这么多事情发生,我很难确切地告诉什么去哪里。Rails非常挑剔,但这似乎通常是我的问题。
即使我在创建它们之后显示它们,删除也不会发生,直到我刷新之后,是的,我尝试了方法::delete,数据:{turbo_method::delete}。如果我能弄明白这一点一次,我会在此后,但它只是没有意义。
这是我的代码,我甚至会把一个链接放到我的GitHub上,以防有人想运行它,看看我到底在说什么。我相信现在有很多错误,就像我一直在改变事情一样。
https://github.com/gagegolden00/Turbo_Issues
控制器

class TodosController < ApplicationController
  before_action :set_todo, only: [:show, :edit, :update, :destroy]
  
  def index
    @todos = Todo.all
    @todo = Todo.new
  end
  
  def show
  end
  
  def new
    @todo = Todo.new
  end
  
  def create
    @todo = Todo.new(todo_params)
    if @todo.save
      render turbo_stream: turbo_stream.append('todos', partial: 'todo_partial', locals: { todo: @todo }),
             notice: 'Task was successfully created.'
    else
      render :new
    end
  end
  
  def update
    if @todo.update(todo_params)
      render turbo_stream: turbo_stream.replace('todos', partial: 'todo_partial', locals: { todo: @todo }),
             notice: 'Task was successfully updated.'
    else
      render :edit
    end
  end
  
  def edit
  end
  
  def destroy
    @todo.destroy
    render turbo_stream: turbo_stream.remove('todos', partial: 'todo_partial', locals: { todo: @todo }),
           notice: 'Task was successfully deleted.'
  end
  
  private
  
  def set_todo
    @todo = Todo.find(params[:id])
  end
  
  def todo_params
    params.require(:todo).permit(:name, :description, :status)
  end
end

表格部分

<!-- _form_partial.html.erb -->

\<%= form_for(@todo, remote: true, html: { class: 'bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4' }) do |f| %\>

  
    <%= f.label :name, class: 'block text-gray-700 text-sm font-bold mb-2' %>
    <%= f.text_field :name, class: 'shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline' %>
  
  
    <%= f.label :description, class: 'block text-gray-700 text-sm font-bold mb-2' %>
    <%= f.text_area :description, class: 'shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline' %>
  
  
    <%= f.label :status, class: 'block text-gray-700 text-sm font-bold mb-2' %>
    <%= f.select :status, options_for_select(Todo.statuses.keys.to_a, @todo.status), {}, class: 'shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline' %>
  
  
    <%= f.submit 'Submit', class: 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline' %>
  
<% end %>

TODO部分

<!-- _todo_partial.html.erb -->

\<%= turbo_frame_tag 'todos' do %\>

  
    <h3>
      <%= todo.name%>
    </h3>
    <p>Description: <%= todo.description %></p>
    Status: <%= todo.status %>
  
<% end %>

TODOS索引视图

<h1>Index: List all todos</h1>

  <!-- List all the todo items -->

  
    <% @todos.each do |todo| %>
      <%= render partial: 'todo_partial', locals: { todo: todo } %>
    <% end %>
  

  <!-- Provide a form to create a new todo item -->

  
    <%= render partial: 'form_partial', locals: { todo: @todo } %>

TODOS SHOW VIEW

<h1>Show: Specific details about a certain todo</h1>
<!-- Display the details of the todo item -->
<p><strong>Title:</strong> <%= @todo.name %></p>
<p><strong>Description:</strong> <%= @todo.description %></p>
<p><strong>Status:</strong> <%= @todo.status %></p>

<!-- Provide a link to edit the todo item -->

\<%= link_to "Edit", edit_todo_path(@todo), remote: true %\>
\<%= button_to "Delete", todo_path(@todo), data: { turbo_method: :delete }, remote: true %\>
x6h2sr28

x6h2sr281#

当您操作视图的todo框架时,框架应该是单个todo元素,而不是整个todo列表。

# create method
render turbo_stream: turbo_stream.append('todos', turbo_frame_tag(@todo, partial: 'todos/todo', locals: { todo: @todo }))

# update method
render turbo_stream: turbo_stream.replace(@todo, partial: 'todos/todo', locals: { todo: @todo })

# destroy method
render turbo_stream: turbo_stream.remove(@todo)

在todo_partial.html.erb中,帧id对于每个todo都应该是唯一的,而不仅仅是'todos'。

<!-- _todo_partial.html.erb -->

<%= turbo_frame_tag dom_id(todo) do %>
  <h3>
    <%= todo.name%>
  </h3>
  <p>Description: <%= todo.description %></p>
  Status: <%= todo.status %>
<% end %>

你可以在这里找到更多的例子:https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/frames_helper.rb
我还发现这个tutorial from hotrails.dev非常简单。
我很抱歉,如果我的代码不匹配您的100%。我希望这有助于开始。

相关问题