Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails helper - how can I get a helper to give me a `<br/>` (or other markup)

My helper works like this:

def some_help(in_string)
  in_string + " and more"
end

But I want it do to a
before the output and I keep getting the < br > characters themselves literally, i.e. not a break but what I want is a < br > that is the problem.

so

def some_help(in_string)
  "<br/>" + in_string + " and more"
end

doesn't work right.

like image 451
junky Avatar asked Apr 30 '12 17:04

junky


People also ask

What do helpers do in Rails?

What is a helper? Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better.

Where are custom view helpers stored within a standard Rails app?

Custom helpers for your application should be located in the app/helpers directory.


2 Answers

Use tag(:br) instead of "<br/>".

content_tag(:br) creates opening and closing br tags and using raw or html_safe is just ugly (not to mention dangerous).

like image 150
Ashitaka Avatar answered Oct 17 '22 23:10

Ashitaka


you can also use the "content_tag" view helper.

http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag

def some_help
   content_tag(:br) + "some help"
end
like image 42
heavysixer Avatar answered Oct 17 '22 23:10

heavysixer