Home > other >  Ruby on Rails: error on concatenating string datatype field and boolean datatype field
Ruby on Rails: error on concatenating string datatype field and boolean datatype field

Time:08-27

I'm working on a Rails HAML View and I'm trying to display together two fields: the first one is String datatype and the other one is Boolean datatype, I'm trying with this line of code:

      %td=students.name   " "   students.status ? '(Passed)' : ''

As you can see I'm using a ternary operator to cast true to Passed and false to blank.

However, I'm getting this error:

error_message

Also, if I avoid ternary operator I got the same output error.

My question is: how can I cast these two fields and at the same time show "passed" if it is true?

Thanks a lot

CodePudding user response:

May be interpolation instead of concatenation?

And don't need ternary operator in that case because nil.to_s == ""

%td="#{students.name} #{'(Passed)' if students.status}"

or even just

%td #{students[:name]} #{'(Passed)' if students[:status]}

CodePudding user response:

try this:

%td=students.name   " "   (students.status ? '(Passed)' : '' )
  • Related