Skip to content

Instantly share code, notes, and snippets.

@Jamedjo
Last active December 31, 2015 20:49
Show Gist options
  • Save Jamedjo/8042502 to your computer and use it in GitHub Desktop.
Save Jamedjo/8042502 to your computer and use it in GitHub Desktop.
Angularjs compatible link_to. Adds target="_self" to all links created with link_to, so $location doesn't hijack the link. Useful if an external gem/engine is using link_to.

Angularjs link_to

Angular compatible link_to. Turns

<%= link_to "Sign up", "/users/sign_up" %>

into

<a href="/users/sign_up" target="_self">Sign up</a>
require "#{Rails.root}/lib/angular_helpers"
module ActionView
module Helpers
module UrlHelper
def link_to_with_angular(name = nil, options = nil, html_options = nil, &block)
if block_given?
options = (options || {}).merge(target: "_self")
else
html_options = (html_options || {}).merge(target: "_self")
end
link_to_without_angular(name, options, html_options, &block)
end
alias_method_chain :link_to, :angular
end
end
end
require 'action_view'
require "./lib/angular_helpers"
describe ActionView::Helpers::UrlHelper do
include described_class
it 'creates links with target _self' do
expect(link_to('a','b')).to match /target="_self"/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment