Why won't ternary operator work with redirect

前端 未结 2 584
时光取名叫无心
时光取名叫无心 2021-01-23 16:09

I love the ternary operator. It really cleans up the code look, but I have a case where it won\'t behave correctly in a Rails controller.

I get a syntax error: une

相关标签:
2条回答
  • 2021-01-23 16:38

    Because of the implied nature of the hash

    Ruby / Rails will imply your argument to redirect is a hash which has some awkward implications in your example

    When ruby implies the arguments to redirect it sees it as a hash in the following scenario it is parsing it as

    nexti==0 ? redirect_to({:back, : redirect_to edit_playt_path(id: actform['playt_id'], i: nexti})
    

    which is invalid, it should work if you explicitly define your hash / argument

    nexti==0 ? redirect_to(:back) : redirect_to(edit_playt_path({id: actform['playt_id'], i: nexti}))
    

    most ruby / rails devs will tell you to avoid ternaries for reasons like this, and also general human comprehension of what is going on. Ruby was ment to be an expressive language, so embrace it.

    return redirect_to(:back) if nexti==0
    redirect_to(edit_playt_path({id: actform['playt_id'], i: nexti}))
    
    0 讨论(0)
  • 2021-01-23 16:43

    You need to use parentheses in your call to redirect_to so the parser can correctly understand what’s going on:

    nexti==0 ? redirect_to(:back) : redirect_to(edit_playt_path(id: actform['playt_id'], i: nexti))
    
    0 讨论(0)
提交回复
热议问题