Ruby ternary operator and method call

后端 未结 1 1058
慢半拍i
慢半拍i 2021-01-29 12:33

I am using ruby 2.1.5, facing some problem with ternary operator

Syntax error

request.xhr?  ? render :json => \"success\"  : redirect_to index_url


        
相关标签:
1条回答
  • 2021-01-29 13:08

    When you use the shorthand syntax (without brackets), ruby expects everything until the end of the line to be parameters to your method. So your "syntax error" example is understood as:

    request.xhr?  ? render(:json => "success"  : redirect_to index_url)
    

    which is obviously wrong.

    0 讨论(0)
提交回复
热议问题