简体   繁体   中英

Rails 4 - HTML in Helper Returning a Hash

Trying to create a simple helper that returns this to my view:

<hr id="effects">

The helper I created looks like this:

def bar(id)
  content_tag(:hr, id: id)
end

When I call

<%= bar("effects") %>

The helper correctly displays the <hr> but the id is being displayed directly to the page like {:id=>"effects"}

I am assuming I am doing this wrong, but can't seem to find anything that can help me. I appreciate any help.

content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)

the second param is content, you can try

def bar(id)
  content_tag(:hr, "", id: id)
end

As per the documentation

def bar(id)
  content_tag(:hr, "", id: id)
end

However, I wonder why you have created a helper method for this. You can do it directly on views (its recommended also).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM