如何从Rails帮助器方法返回多个值

时间:2015-04-19 11:06:44

标签: ruby-on-rails link-to

我有一个铁路助手,我有两个link_to并排。如果友谊状态为requested。它只显示第二个链接。我如何格式化这两个链接显示?

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

2 个答案:

答案 0 :(得分:2)

连接字符串。块的返回值将是返回的最后一行;您必须连接两个link_to调用的返回值。

  def action_buttons(user)
    case current_user.friendship_status(user)
    # ...
    when "requested"
      link_to("Accept", ...) +
      link_to("Decline", ...)
      # ...
    end
  end

答案 1 :(得分:1)

如果它适用于您的特定情况,则接受的答案很好,但它不是很强大。这不是干,因为您在其他一些地方重复link_toclass属性。如果您需要操纵结果,例如添加任何其他标记,它就会崩溃。比如,说你想制作链接列表项目。

如果你长大了它的作用,我可能会建议让你的助手返回你的模板然后使用的数据:

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

然后在模板中,您只需遍历数组并从返回的哈希中提取适当的属性。

或者,你可以让你的助手拿一个块,然后屈服于那个块:

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_to分解出来,以减少重复次数。