为什么三元运算符不能使用重定向

时间:2021-09-04 01:09:21

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.

我喜欢三元运算符。它确实清理了代码外观,但我有一种情况,它在Rails控制器中无法正常运行。

I get a syntax error: unexpected tSYMBEG, expecting keyword_do or '{' or '('

我收到语法错误:意外的tSYMBEG,期待keyword_do或'{'或'(''

I consistently get this issue, it light switches on changing the statement below to a ternary operator. It always happens when I'm trying to use it in conjunction with a redirect statement.

我一直都遇到这个问题,它开启了将下面的声明更改为三元运算符。当我尝试将它与重定向语句结合使用时,它总会发生。

Am I unaware of a rule about this?

我不知道这个规则吗?

if nexti==0 then
  redirect_to :back
else
  redirect_to edit_playt_path(id: actform['playt_id'], i: nexti)
end

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

2 个解决方案

#1


2  

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

Ruby / Rails意味着你的重定向参数是一个哈希,它在你的例子中有一些尴尬的含义

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

当ruby暗示重定向的参数时,它将其视为下一个场景中的哈希,它将其解析为

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.

大多数ruby / rails开发人员会告诉你出于这样的原因避免使用三元组,以及人们对正在发生的事情的一般理解。 Ruby是一种富有表现力的语言,所以接受它。

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

#2


1  

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

您需要在调用redirect_to时使用括号,以便解析器可以正确理解正在发生的事情:

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

#1


2  

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

Ruby / Rails意味着你的重定向参数是一个哈希,它在你的例子中有一些尴尬的含义

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

当ruby暗示重定向的参数时,它将其视为下一个场景中的哈希,它将其解析为

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.

大多数ruby / rails开发人员会告诉你出于这样的原因避免使用三元组,以及人们对正在发生的事情的一般理解。 Ruby是一种富有表现力的语言,所以接受它。

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

#2


1  

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

您需要在调用redirect_to时使用括号,以便解析器可以正确理解正在发生的事情:

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