Rails link_to_if 没有正确隐藏链接
Rails link_to_if not hiding links correctly
出于某种原因,我的 link_to_if 行有效,但出现在我的模型(公司)的每个显示视图中。
代码如下:
<% @customers.each do |customer| %>
<li>
<%= link_to_if customer.company_id == @company.id, "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>
问题:我已将 Customer1 link 发送到 CompanyX。当我转到 CompanyZ 时,它显示 Customer1,但 link 不是 hyperlink。它只是明文,甚至不应该出现。但是,在 CompanyX 看来,link 工作正常。我在这里做错了什么?
如果你阅读 link_to_if
(https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if) 的文档,它清楚地表明 [if false] only the name is returned.
在文档中,您可以发现给定的(可选)块是在 false
情况下呈现的。所以在你的情况下你可以传递一个空块:
<%= link_to_if false, customer_path(customer[:id]) {} %>
在我看来,如果您只想在 @customers
中的一个或多个 customer
(s) 与 @company
相关联时显示 link,您应该这样做:
<% @customers.where(company_id: @company.id).each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% %>
如果你想隐藏一些记录,你可以从控制器到控制基于客户的公司
@customers = Company.find(:id).customers
然后在你的视图中你可以只显示它而不用比较它
<% @customers.each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>
出于某种原因,我的 link_to_if 行有效,但出现在我的模型(公司)的每个显示视图中。
代码如下:
<% @customers.each do |customer| %>
<li>
<%= link_to_if customer.company_id == @company.id, "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>
问题:我已将 Customer1 link 发送到 CompanyX。当我转到 CompanyZ 时,它显示 Customer1,但 link 不是 hyperlink。它只是明文,甚至不应该出现。但是,在 CompanyX 看来,link 工作正常。我在这里做错了什么?
如果你阅读 link_to_if
(https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if) 的文档,它清楚地表明 [if false] only the name is returned.
在文档中,您可以发现给定的(可选)块是在 false
情况下呈现的。所以在你的情况下你可以传递一个空块:
<%= link_to_if false, customer_path(customer[:id]) {} %>
在我看来,如果您只想在 @customers
中的一个或多个 customer
(s) 与 @company
相关联时显示 link,您应该这样做:
<% @customers.where(company_id: @company.id).each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% %>
如果你想隐藏一些记录,你可以从控制器到控制基于客户的公司
@customers = Company.find(:id).customers
然后在你的视图中你可以只显示它而不用比较它
<% @customers.each do |customer| %>
<li>
<%= link_to "#{customer.first_name} #{customer.last_name}", customer_path(customer[:id]) %>
</li>
<% end %>