0

I have a simple show method that has to render all the messages in a conversation.

Everything is basic in my code, I have a show method that goes to this HTML (show.html.haml):

%section#list
  =  @conversation.messages.each do |message|
    .row
      = message.content
      = message.participant.user_id

And when I go to this show page, I have my content, but I also have a random line from the database (my HTML contains nothing else). Here is what I get :

This is a test 1 

[#< MyghtDefaultMessenger::Message id: 1, content: "This is a test", deleted: false, participant_id: 1, created_at: "2017-02-13 14:52:45", updated_at: "2017-02-13 14:52:45" >] 

(I added a blank space right next to the < and > signs else SO would just render, but they aren't here in the original. [#] )

Here is my controller method in conversation_controller :

def show
  @conversation.messages.order(:created_at)
end

And the only specificity is that my conversation has many participants, my participants have many messages, and I get the messages of a conversation through participants.

1
  • 1
    Never used HAML so this is a guess. I think its because of this = in = @conversation.messages.each do |message| . In ERB = is not used when you dont want to print the output of that particular ruby code, for example. each block. Commented Feb 21, 2017 at 9:36

1 Answer 1

1

You need to change = with -

  • = prints the result of evaluated code
  • - will just evaluate it

Change the code to

%section#list
  - @conversation.messages.each do |message|
    .row
      = message.content
      = message.participant.user_id

The second output you are getting is the result which is returned after the @conversation.messages.each do |message| is evaluated.

NOTE: Use - whenever you just need to evaluate for e.g. each, if - else, Assigning variable in haml

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

1 Comment

Wow thanks a lot it looks ridiculous now that you said it but when you don't know it.. thanks again

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.