如何从 Rails 辅助方法中 return 多个值
How to return multiple values from a Rails helper method
我有一个 rails 助手,我有两个并排的 link_to
。如果好友状态为requested
。它只显示第二个 link。我该如何设置格式以便同时显示 link?
def action_buttons(user)
case current_user.friendship_status(user) when "friends"
link_to "Cancel Friendship", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs"
when "pending"
link_to "Cancel Request", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs"
when "requested"
link_to "Accept", accept_friendship_path(current_user.friendship_relation(user)), method: :put, class: "btn btn-primary btn-xs"
link_to "Decline", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-default btn-xs"
when "not_friends"
link_to "Add as Friend", friendships_path(user_id: user.id), method: :post, class: "btn btn-primary btn-xs"
end
end
连接字符串。该块的 return 值将是最后一行 returned;您必须连接两个 link_to
调用的 return 值。
def action_buttons(user)
case current_user.friendship_status(user)
# ...
when "requested"
link_to("Accept", ...) +
link_to("Decline", ...)
# ...
end
end
接受的答案如果适用于您的特定情况则很好,但不是很可靠。它不是 DRY,因为您在许多其他地方重复 link_to
和 class
属性。如果您需要操纵结果,例如添加任何其他标记,它就会分崩离析。比如,假设您想制作 links 列表项。
如果您不再需要它为您做的事情,我可能建议让您的助手 return 模板随后使用的数据:
def action_buttons(user)
case current_user.friendship_status(user) when "friends"
[
{
label: "Cancel Friendship",
path: friendship_path(current_user.friendship_relation(user)),
form_attributes: { method: :delete }
}
]
when "pending"
# likewise
when "requested"
[
{
label: "Accept",
path: accept_friendship_path(current_user.friendship_relation(user)),
form_attributes: { method: :put }
}, {
label: "Decline",
path: friendship_path(current_user.friendship_relation(user)),
form_attributes: { method: :delete }
}
]
when "not_friends"
# likewise
end
end
然后在模板中,您只需遍历数组并从 returned 的散列中提取适当的属性。
或者,您可以让您的助手拿走一个街区,然后屈服于那个街区:
def action_buttons(user)
case current_user.friendship_status(user) when "friends"
yield(link_to "Cancel Friendship", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs")
when "pending"
yield(link_to "Cancel Request", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs")
when "requested"
yield(link_to "Accept", accept_friendship_path(current_user.friendship_relation(user)), method: :put, class: "btn btn-primary btn-xs")
yield(link_to "Decline", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-default btn-xs")
when "not_friends"
yield(link_to "Add as Friend", friendships_path(user_id: user.id), method: :post, class: "btn btn-primary btn-xs")
end
end
这样,模板只是提供了对块中每个link进行的操作。如果您想要的只是连接,那么该块可能只是 { |link| link }
,但您也可以将其包装在标签或其他内容中。我仍然建议分解 link_to
以减少重复。
我有一个 rails 助手,我有两个并排的 link_to
。如果好友状态为requested
。它只显示第二个 link。我该如何设置格式以便同时显示 link?
def action_buttons(user)
case current_user.friendship_status(user) when "friends"
link_to "Cancel Friendship", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs"
when "pending"
link_to "Cancel Request", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs"
when "requested"
link_to "Accept", accept_friendship_path(current_user.friendship_relation(user)), method: :put, class: "btn btn-primary btn-xs"
link_to "Decline", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-default btn-xs"
when "not_friends"
link_to "Add as Friend", friendships_path(user_id: user.id), method: :post, class: "btn btn-primary btn-xs"
end
end
连接字符串。该块的 return 值将是最后一行 returned;您必须连接两个 link_to
调用的 return 值。
def action_buttons(user)
case current_user.friendship_status(user)
# ...
when "requested"
link_to("Accept", ...) +
link_to("Decline", ...)
# ...
end
end
接受的答案如果适用于您的特定情况则很好,但不是很可靠。它不是 DRY,因为您在许多其他地方重复 link_to
和 class
属性。如果您需要操纵结果,例如添加任何其他标记,它就会分崩离析。比如,假设您想制作 links 列表项。
如果您不再需要它为您做的事情,我可能建议让您的助手 return 模板随后使用的数据:
def action_buttons(user)
case current_user.friendship_status(user) when "friends"
[
{
label: "Cancel Friendship",
path: friendship_path(current_user.friendship_relation(user)),
form_attributes: { method: :delete }
}
]
when "pending"
# likewise
when "requested"
[
{
label: "Accept",
path: accept_friendship_path(current_user.friendship_relation(user)),
form_attributes: { method: :put }
}, {
label: "Decline",
path: friendship_path(current_user.friendship_relation(user)),
form_attributes: { method: :delete }
}
]
when "not_friends"
# likewise
end
end
然后在模板中,您只需遍历数组并从 returned 的散列中提取适当的属性。
或者,您可以让您的助手拿走一个街区,然后屈服于那个街区:
def action_buttons(user)
case current_user.friendship_status(user) when "friends"
yield(link_to "Cancel Friendship", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs")
when "pending"
yield(link_to "Cancel Request", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-primary btn-xs")
when "requested"
yield(link_to "Accept", accept_friendship_path(current_user.friendship_relation(user)), method: :put, class: "btn btn-primary btn-xs")
yield(link_to "Decline", friendship_path(current_user.friendship_relation(user)), method: :delete, class: "btn btn-default btn-xs")
when "not_friends"
yield(link_to "Add as Friend", friendships_path(user_id: user.id), method: :post, class: "btn btn-primary btn-xs")
end
end
这样,模板只是提供了对块中每个link进行的操作。如果您想要的只是连接,那么该块可能只是 { |link| link }
,但您也可以将其包装在标签或其他内容中。我仍然建议分解 link_to
以减少重复。