Rails 地址格式助手
Rails Address Format Helper
我正在尝试创建一个帮助程序,它将格式化来自 5 列(add1、add2、add3、add4、add5)的地址,以便它压缩所有空值并在屏幕上显示
Add1,
Add3,
Add4,
Add5
(添加 2 被跳过,因为它为空)
我为 assemble 创建了以下助手并跳过任何空值,但我无法引入新的换行符。有什么想法吗?
def delivery_address(customer)
@customer = Customer.find(customer.id)
if @customer.del_address1.blank?
deladdress = "No Delivery Address"
else
deladdress = @customer.del_address1
deladdress = deladdress + "</br>"
if customer.del_address2.blank?
else
deladdress = deladdress + @customer.del_address2 + ","
end
if @customer.del_address3.blank?
else
deladdress = deladdress + @customer.del_address3 + ","
end
if @customer.del_address4.blank?
else
deladdress = deladdress + @customer.del_address4 + ","
end
if @customer.del_address5.blank?
else
deladdress = deladdress + @customer.del_address5
end
end
end
def delivery_address(customer)
@customer = Customer.find(customer.id)
return "No Delivery Address" if @customer.del_address1.blank?
[
@customer.del_address1,
@customer.del_address2,
@customer.del_address3,
@customer.del_address4,
@customer.del_address5
].reject(&:blank?).join(",\n")
end
无需编写 5 if
条语句,您可以将所有值放入一个数组中,然后删除任何空白值。
请注意,我的代码是基于您的代码,但与您的问题所要求的不完全相同:
it will compact any nulls
空字符串也被视为 blank?
(但 "" != nil
)。所以这个方法也会跳过这些值。
如果您只想忽略 nil
值,但保留空字符串,请将 reject(&:blank?)
替换为 compact
。
一个重要的建议:你不应该在视图助手中进行任何数据库查询。这应该只在控制器中完成。活动记录应该已经传递到辅助函数中。所以这一行
@customer = Customer.find(customer.id)
可以删除。
def delivery_address(customer)
# Use a guard clause, this reduces if nesting
return 'No Delivery Address' if customer.del_address1.blank?
# Collect all parts
parts = [customer.del_address1, customer.del_address2, customer.del_address3, customer.del_address4, customer.del_address5]
# Remove all empty parts.
# Using a bang method is lighter on memory allocation
# and will speed up performance.
parts.reject!(&:blank?)
parts.join('<br/>').html_safe
end
我正在尝试创建一个帮助程序,它将格式化来自 5 列(add1、add2、add3、add4、add5)的地址,以便它压缩所有空值并在屏幕上显示
Add1,
Add3,
Add4,
Add5
(添加 2 被跳过,因为它为空)
我为 assemble 创建了以下助手并跳过任何空值,但我无法引入新的换行符。有什么想法吗?
def delivery_address(customer)
@customer = Customer.find(customer.id)
if @customer.del_address1.blank?
deladdress = "No Delivery Address"
else
deladdress = @customer.del_address1
deladdress = deladdress + "</br>"
if customer.del_address2.blank?
else
deladdress = deladdress + @customer.del_address2 + ","
end
if @customer.del_address3.blank?
else
deladdress = deladdress + @customer.del_address3 + ","
end
if @customer.del_address4.blank?
else
deladdress = deladdress + @customer.del_address4 + ","
end
if @customer.del_address5.blank?
else
deladdress = deladdress + @customer.del_address5
end
end
end
def delivery_address(customer)
@customer = Customer.find(customer.id)
return "No Delivery Address" if @customer.del_address1.blank?
[
@customer.del_address1,
@customer.del_address2,
@customer.del_address3,
@customer.del_address4,
@customer.del_address5
].reject(&:blank?).join(",\n")
end
无需编写 5 if
条语句,您可以将所有值放入一个数组中,然后删除任何空白值。
请注意,我的代码是基于您的代码,但与您的问题所要求的不完全相同:
it will compact any nulls
空字符串也被视为 blank?
(但 "" != nil
)。所以这个方法也会跳过这些值。
如果您只想忽略 nil
值,但保留空字符串,请将 reject(&:blank?)
替换为 compact
。
一个重要的建议:你不应该在视图助手中进行任何数据库查询。这应该只在控制器中完成。活动记录应该已经传递到辅助函数中。所以这一行
@customer = Customer.find(customer.id)
可以删除。
def delivery_address(customer)
# Use a guard clause, this reduces if nesting
return 'No Delivery Address' if customer.del_address1.blank?
# Collect all parts
parts = [customer.del_address1, customer.del_address2, customer.del_address3, customer.del_address4, customer.del_address5]
# Remove all empty parts.
# Using a bang method is lighter on memory allocation
# and will speed up performance.
parts.reject!(&:blank?)
parts.join('<br/>').html_safe
end