1

I am looping through an array in view, it works, but when I tried to move the code to helper method, it did not work. The problem is I am not able to do <%= %> inside helper. Anybody could let me know how to fix the code?

original view

<% resources.each do |resource| %>
  <span class="label label-primary">
    <%= resource.name %>
  </span>
<% end %>

updated view

<%= print_resource resources%>

resource_helper.rb

def print_resource(resources)
  resources.each do |resource|
    text = resource.name
    clz = 'label label-primary'
    content_tag :span, text,clz
  end
end
2
  • it is return nothing right ? Commented Dec 12, 2016 at 6:55
  • @Thorin return like this ["<span class=\"label label-primary\">Update </span>", "<span class=\"label label-primary\">Delete</span>"'] Commented Dec 12, 2016 at 6:57

1 Answer 1

2

Just do like this:

def print_resource(resources)
  html_values = ""
  resources.each do |resource|
    text = resource.name
    clz = 'label label-primary'
    html_values << (content_tag :span, text,clz)
  end
 html_values.html_safe
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works for me. Could you let me know where I can learn things like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.