1

I am quite new in Ruby-on-rails. And I have a very simple question: This one works in my view file:

- @posts.each do |child|
    = child.name
end

However, I can't create a helper method which takes an array as argument and iterate it with 'each'.

In my view file:

=f1(@posts)

In my helper file:

def f1(abc)
    abc.each do |child|
            puts child.name
    end
end

Please help me out with this silly problem. Thanks a lot in advance

2 Answers 2

1

Does something like this work?

def f1(abc)
  output = ''
  abc.each do |child|
    output << "<br/> #{child.name}"
  end
  output
end
Sign up to request clarification or add additional context in comments.

Comments

0

In a running rails app puts outputs into the log file.

I recommend trying this approach:

Create a '_post' partial in the posts view directory, then put what you want to render out using post as the object (ie: post.name) then use render @posts in the view. This will automagically render the partial for each object in the collection.

See section 3.4.5 in the Ruby on Rails Layouts and Rendering Guide for more info

Comments

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.