Search code examples
ruby-on-railslink-to

How to return multiple values from a Rails helper method


I have a rails helper where I have two link_to's side by side. If the friendship status is requested. It only displays the second link. How do I format this so both links show?

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

Solution

  • Concatenate the strings. The return value of the block will be the last line returned; you must concatenate the return values of the two link_to calls.

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