Rails link_to how to show blank when null -
i have in view:
<td><%= link_to product_sale.product.consignor.try(:name), { controller: :consignors, action: :edit, id: product_sale.product.consignor.try(:id) }, :target => "_blank" %></td>
which in product_sale view links edit consignors products listed.
some product doesn't have consignor. when happens, shows /consignors/edit
how can show blank instead?
when show blank instead mean not show link? if so, can't conditionally call link_to helper:
<td><%= product_sale.product.consignor ? (link_to edit_consignor_path(product_sale.product.consignor) ) : '' %></td>
as long routes set correctly shouldn't need specifying controller
, action
, id
either.
if want display more meaningful end user instead of default link add parameter before edit path:
<td><%= product_sale.product.consignor ? (link_to product_sale.product.consignor.name, edit_consignor_path(product_sale.product.consignor) ) : '' %></td>
and maybe consider setting variable readability:
<td><% consignor = product_sale.product.consignor %> <%= consignor ? (link_to consignor.name, edit_consignor_path(consignor) ) : '' %></td>
if aren't interesting in displaying other blank when no consignor set replace tertiary operator if statement suggested 2called-chaos:
<td><% consignor = product_sale.product.consignor %> <%= link_to consignor.name, edit_consignor_path(consignor) if consignor %></td>
Comments
Post a Comment