Search code examples
ruby-on-railsrubylink-to

ruby rails delete space after link_to without HAML


I am programming an app with Ruby on Rails and, in some views, I would like to get rid of the automatic whitespace triggered by the 'link_to' helper.

<%= link_to liker.first_name.capitalize, user_path(liker) %>
   <!-- Example of link with a user's firstname (who liked a given content), redirecting to it's user profile -->

I have read this post, which talks about using HAML (that I do not use). Isn't it possible to delete this tiny whitespace only using Ruby on Rails?


Solution

  • You always get a whitespace between inline html elements if you put them in different lines in your html document. For example

    <span>foo</span>
    <span>bar</span>
    

    would render as "foo bar" on the page. But if you write that html elements next to each other in one line, without a whitespace in between

    <span>foo</span><span>bar</span>
    

    it would render as "foobar".

    That said: Just write the link_to into the same line with the text that should not be separated with a whitespace:

    foo<%= link_to 'bar', '#' %>baz
    

    Would look like "foobarbaz" without any whitespace