htamayo
htamayo

Reputation: 353

Ruby on Rails: error on concatenating string datatype field and boolean datatype field

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

Upvotes: 0

Views: 120

Answers (2)

mechnicov
mechnicov

Reputation: 15248

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]}

Upvotes: 1

Les Nightingill
Les Nightingill

Reputation: 6156

try this:

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

Upvotes: 0

Related Questions