Using Helpers in Rails 3 to Output HTML

Using helpers in rails 3 to output html

I agree with the comment above recommending the use of a partial... but if you DID need to do this in a helper, this is a cleaner way to implement:

def display_all(collection)
content_tag(:ul, class: "list") do
collection.collect do |member|
concat(content_tag(:li, id: member.name.gsub(' ', '-').downcase.strip) do
member.name
end)
end
end
end

I'd pass in a collection explicitly rather than passing in a symbol to create a collection so you aren't always required to display ALL the records in a particular table at once. You could add pagination, etc.

Rails 3: Where should a helper that uses h (i.e. html_escape) live?

Place the logic that does your view assembly into a CardHelper:

app/helpers/card_helper.rb

class CardHelper
def rules(card)
initial_text = h card.rules_text
# assembles HTML output based on initial_text and fields of card
output_text.html_safe
end
end

It's not clear from your example whether you want to format several fields via the format method. If that's the case, then you might be able to do:

class CardHelper
def format(card, attribute)
initial_text = h card[attribute]
# assembles HTML output based on initial_text and fields of card
output_text.html_safe
end
end

You can use this helper like any other:

class CardsController
helper CardHelper
end

and in your views:

<%= rules(@card) %>

or

<%= format(@card, :rules) %>

Ruby on Rails Helper Method - HTML is displayed as plain text

The result of your helper needs to be marked as "html_safe" in Rails 3. Otherwise, the tags will be escaped.

def my_helper
data = "<p>Hello!</p>"
data.html_safe
end

By default helpers in rails 3 generates HTML5 code, is there a way to make them output htm4 code

Rails provides helpers to create html5 tags like email_tag, number_tag etc.
The best way to keep your html compatible to html4 version , we should either avoid using this new helpers or override them to render html4 code.

The issue if you use these new tags is in html5 supportive browsers you would see the different behaviour than in the one that does not support them. So its better you don't use these tag, unless most of the browsers start supporting html5 completely

How to get HTML tag included in the output using a helper method

Just concat any tags you want into the output.

def get_hotel_theme_names(hotel)
output = []
hotel.themes.each do |theme|
output << (theme.theme_names + link_to('(X)', theme, :method=>:delete, :confirm=>'sure?'))
#or add other tags you want
#output << '<div>test</div>'
end

output.join(',').html_safe
end

How does a helper method yield to a block in rails 3?

In Rails 3, you no longer need to use the concat method to build the content of your blocks.

Your helper now looks like this:

def box_wrapper(&block)  
content = capture(&block)
content_tag(:div, content, :class => 'box')
end

You can see other examples in Railscasts 208.

Using helpers in a view escapes the html?

You probably want to call html_safe before you return. The sanitization behavior changed a bit in Rails 3 (XSS protection was enabled by default), so you may also want to check out this SO discussion of raw, h, and html_safe, which links to Yehuda Katz's explanation of SafeBuffers in Rails 3.



Related Topics



Leave a reply



Submit