【问题标题】:rails 7 redirect not occurringrails 7重定向未发生
【发布时间】: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


【解决方案1】:

编辑

下面的答案是固执己见的:我假设您想使用 Turbo 帧/流,因为您将员工封闭在“员工”turbo 框架中,并且您的控制器中有一个 turbo_stream 方法。

虽然它没有清楚地回答你的问题。当您点击员工时,URL 会发生变化这一事实需要调查。

请分享您的show.html.erb 文件以获得更好的照片。


您在使用 Turbo Frame 时并没有考虑到单页原则。

您目前只有一个框架,称为“员工”。您的所有员工卡片都以纯 HTML 格式添加。基本上在 div 上添加 dom_id 将没有任何效果,因为 Turbo(无论是通过 HTML 响应还是 Turbo_stream 响应)无法删除任何不是 turbo Frame 的 DOM 元素。

当您从索引页面中删除一张卡片时,我认为会发生索引页面被重放并且闪现的情况。就像旧的 Rails pre-Turbo 时代一样。

在您的控制器中,您没有说是否有 destroy.turbo_stream.erb 文件,但我对此表示怀疑。如果你有它无论如何都没有效果。

现在,关于您的显示视图,这很有趣,因为代码似乎与您的 _employee.html.erb 部分不匹配。然后,您可能在 show.html.erb 中有一些自定义 HTML,以便制作更详细的配置文件。但是这个详细的配置文件也可能没有涡轮框架。

该怎么办

  • 在您的 _employee.html.erb 文件中,将 &lt;div id="&lt;%= dom_id employee %&gt;"&gt; 替换为 &lt;%= turbo_frame_tag dom_id(employee) do %&gt; 和 将关闭的&lt;/div&gt; 替换为关闭的&lt;% end %&gt;。你所有的 员工将拥有自己的涡轮增压框架。

  • 创建一个destroy.turbo_stream.erb 文件并在里面添加&lt;%= turbo_stream.remove "employee_#{@employee.id}" %&gt;。这意味着,不是重新加载您的索引页面,或者基本上使用正在加载的新页面导航您的应用程序,而是从视图中删除正确的 Turbo 框架。你基本上没有离开你的视野。对雇员#destroy 的调用已由 Turbo 异步完成。

现在关于您的详细视图:

似乎一切都在表演中。最好创建另一个名为 _detailed_employee.html.erb 的部分并添加相同的涡轮框架 &lt;%= turbo_frame_tag dom_id(employee) %&gt;

当你销毁它时,它应该像上面详述的那样从 DOM 中删除。

请注意,当您在节目中删除此卡片时,您的节目视图将留空。这可能不是你想要的。所以你以后可能不得不调整你的逻辑。

此外,您的代码目前是混合的:您同时利用 Turbo 框架(SPA 概念)和经典导航。一切都很好,您稍后可能会找到正确的平衡点。但是,即使对于有经验的程序员来说,也很难将他们的思想集中在以 HTTP 方式实际访问页面并使用 Turbo 执行一些异步请求的概念上。

我希望我的帖子没有太多错误,并且对您有所帮助。

PS:差点忘了:对于您的链接,您不需要重复员工两次employee_path(employee, employee) 它使导航栏中的路径看起来像employees/14.14 Rails 知道 ID 参数是 14,但不需要第二个 14。

【讨论】:

    猜你喜欢
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2022-11-13
    • 2014-12-23
    • 1970-01-01
    • 2023-02-25
    相关资源
    最近更新 更多