There is no built-in way to do this that I know of. I would probably create a helper method if it does a lot of things.
def anchor_to(link_text, object) link_to(link_text, "##{object.class.name.underscore}_#{object.id}") end
You can make the method more complicated if you need to handle more cases (passing parameters to link_to, etc.), but something simple would clear them. Link building will:
= anchor_to("Link to Billy", @user)
If you want to use Haml helpers, you can do something very similar (but much more confusing):
def anchor_to(link_text, object) capture_haml do haml_tag :a, 'Link to Billy', href: "##{object.class.name.underscore}_#{object.id}" end end
Although it should be warned that the underscore method will not be available if you do this outside of Rails (this is the only reason I can think of avoiding the link_to helper).
Emily source share