user715697
user715697

Reputation: 887

syntax error, unexpected ':'

For some reason I cannot use render in a one line conditional.

Example, this works:

@key = Key.find_by_patient_id(5).nil? ? @key : @key

But this does not:

@key = Key.find_by_patient_id(5).nil? ? render :index : render :index

Error message:

syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' ...y_patient_id(5).nil? ? render :index : render :index

Upvotes: 1

Views: 4851

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96934

It's because the colon is ambiguous whether it represents a symbol or the separator. Grouping each condition should fix the syntax error:

@key = Key.find_by_patient_id(5).nil? ? (render :index) : (render :index)

Upvotes: 2

Related Questions