【发布时间】:2022-09-27 17:51:57
【问题描述】:
我有一个删除方法没有触发我的控制器中指定的重定向的问题。我正在使用 Rails 7.0.4 和 Ruby 3.1.2
员工控制器.rb
def destroy
@employee.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: \"Employee was deleted\" }
format.turbo_stream { flash.now[:notice] = \"Employee was deleted\" }
end
end
_employees.html.erb
<%= turbo_frame_tag \"employees\" do %>
<%= render employees %>
<% end %>
_employee.html.erb
<div id=\"<%= dom_id employee %>\">
<div class=\"index bg-white dark:bg-gray-900 px-4 md:px-10\">
<div class=\"overflow-x-auto\">
<table class=\"w-full whitespace-nowrap\">
<tbody>
<tr tabindex=\"0\" class=\"focus:outline-none text-sm leading-none text-gray-600 dark:text-gray-200 h-16\">
<td class=\"w-1/2\">
<div class=\"flex items-center\">
<div class=\"w-10 h-10 rounded-sm flex items-center justify-center\">
<p class=\"text-xs font-bold leading-3 text-white\"><%= gravatar_for employee %></p>
</div>
<div class=\"pl-2\">
<p><%= link_to \"#{employee.first_name} #{employee.last_name}\", employee_path(employee, employee), class: \"text-sm font-medium leading-none text-gray-800 dark:text-white\"%></p>
<p class=\"text-xs leading-3 text-gray-600 dark:text-gray-200 mt-2\"><%= employee.email %></p>
</div>
</div>
</td>
<td class=\"pl-8\">
<p>
<%= link_to \"View\", employee_path(employee, employee), type: \"button\",
class: \"inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg
focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out\" %>
</p>
</td>
<td class=\"pl-8\">
<p>
<%= link_to \"Edit\", edit_employee_path(employee, employee), type: \"button\",
class: \"inline-block px-6 py-2.5 bg-yellow-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-yellow-700 hover:shadow-lg
focus:bg-yellow-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-yellow-800 active:shadow-lg transition duration-150 ease-in-out\" %>
</p>
</td>
<td class=\"pl-8\">
<p>
<%= link_to \"Delete\", employee_path(employee), data: { turbo_method: :delete, turbo_confirm: \'Are you sure?\' }, type: \"button\",
class: \"inline-block px-6 py-2.5 bg-red-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-red-700 hover:shadow-lg
focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out\" %>
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
我什至录制了一个快速的 Loom,展示了在操作中缺乏重定向: https://www.loom.com/share/e2c333e54fb84b9193c07b54a3546d54
-
新手,从未使用过 Rails 7。如果从控制器中删除
format.turbo_stream会发生什么?我认为它不是用于重定向,而是用于页面内更新,请参阅blog.corsego.com/button-to-html-or-turbo_stream。您也可以尝试更改行的顺序。
标签: ruby-on-rails ruby