1

Suppose I get all the users by @users = User.all, then in my partial I have some loop

%ul
        = users.each do |index|
          = content_tag(:li,index.name)

the problem, is that I get this output

<ul>
    <li>
        Administrator //correct user name 
    <li>
    [#&lt;User id: 1, email: "[email protected]", encrypted_password: "$2a...",...;]
</ul>

Why is the entire object displayed ?

2 Answers 2

4

You should use this

    %ul
      - users.each do |user|
        =content_tag(:li, user.name)

the difference between - and = is that the first wont output but the second will. As so,- = users.each do |user| rendered the entire instance variable (precisely passed as a local)

Sign up to request clarification or add additional context in comments.

3 Comments

@MrYoshiji sorry, copy pasting issue ;)
@MrYoshiji sorry I wanted to edit mine and I put something within. So sorry !
It's okay if you didn't do it on purpose ;)
0

Because of = sign in the beginning of the line. It should be:

%ul
  -users.each do |user|
    =content_tag(:li, user.name)

I used user as an instance variable name passed to the block, for better readability.

3 Comments

yes but I passed @users as a local user in my partial. So how can I manage this ?
@user1611830 You should pass it as users. Look my answer again.
no then I get an error method telling me each is not recognized as a method

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.