0

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?

Community
  • 1
  • 1
Quentin
  • 57
  • 7

2 Answers2

0

As Akshay said you can use .strip or .strip! to remove leading and trailing whitespace.But if you want to remove all whitespace use some tricky hack like .gsub(/\s+/, "") or .gsub(" ","").

<%= link_to liker.first_name.capitalize.to_s.gsub(/\s+/, ""), user_path(liker) %>
linux
  • 23
  • 2
  • 7
  • My 'first_name' attribute was already cleaned in terms of whitespaces (before saving in the DB). The suggestion by @spickermann made the job. Thanks in any case! – Quentin Jul 06 '16 at 15:10
0

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

spickermann
  • 100,941
  • 9
  • 101
  • 131