Juanda

Using view helpers in Rails controllers

The other day I needed to use a view helper in my controller in order to nicely format a flash message. It turns out that it’s a simple thing to do.

In my particular case, I had a helper to format a phone number using the phony gem.

# app/helpers/application_helper.rb
def format_phone(phone_number)
  unless phone_number.nil?
    Phony.formatted(phone_number, spaces: '-')
  else
    ''
  end
end

Once you have your view helper, you can use it in any rails controller by invoking the view_context object

# app/controllers/phones_controller.rb
def confirm
  if current_user.confirmed_phone?
    redirect_to edit_phone_path,
      notice: \
        "The phone number #{view_context.format_phone(current_user.phone)} is already confirmed."
  end
end

You can see more about the view_context object on the api dock.