current_page?在 _partial 视图中不断出错

current_page? in a _partial view keeps erroring

我正在使用 _form 部分,其中包含我的模型产品的创建和编辑模板。

我有以下代码来检查它是否是编辑视图

<% current_page?(edit_product_path(@product)) %>

然而,当我进入创建视图时,网站崩溃并出现以下错误

No route matches {:action=>"edit", :controller=>"products", :id=>nil} missing required keys: [:id]

我找不到问题所在。

edit_product_path() 需要您要编辑的产品的 id,但在您的情况下,不知何故 @productnil,因此,您收到错误.而且,以后有可能又是nil,所以你要打勾:

<% if @product %>
  <% current_page?(edit_product_path(@product) %>
  <%# Your other code %>
<% end %>