Last Updated: February 25, 2016
·
666
· tal

Rails view helpers in controller.

I recently ran into a problem where I wanted a flash message to contain a link. But the link_to method isn't accessible from the controller.

How do you get around it? You can access your view using view_context.

app/controllers/sessions_controller.rb

flash[:alert] = "#{view_context.link_to 'Forgot your password?', forgot_password_path}"

Link:
http://apidock.com/rails/AbstractController/Rendering/view_context