Ari*_*iel 0 ruby jquery ruby-on-rails operators operator-precedence
我正在构建我的第一个Rails应用程序,我正在使用JQuery来发出ajax POST请求来更新资源.我正在发送_method: "PATCH"
并正在执行正确的控制器:
def update
@buddyship = Buddyship.find(params[:id])
if @buddyship.involve? current_user && @buddyship.update(buddyship_params)
render json: { success: true }
else
render json: { success: false }, :status => 500
end
end
Run Code Online (Sandbox Code Playgroud)
我首先测试标准用例,其中关系确实涉及当前用户,因此条件的第一部分评估为true
.第二个也是,我知道,因为我尝试过
def update
@buddyship = Buddyship.find(params[:id])
bool = @buddyship.update(buddyship_params)
logger.debug "bool: "+bool.to_s
if @buddyship.involve? current_user && bool
render json: { success: true }
else
render json: { success: false }, :status => 500
end
end
Run Code Online (Sandbox Code Playgroud)
在我得到的日志中bool: true
.此外,记录仍在数据库中更新.不知怎的,我得到内部服务器错误而不是成功.
如果我只是使用and
而不是&&
,一切都按照我的预期运作.
if @buddyship.involve? current_user && @buddyship.update(buddyship_params)
Run Code Online (Sandbox Code Playgroud)
我知道它的and
优先级低于&&
,并且赋值运算符=
介于两者之间.但在这种情况下,这似乎并不重要,毕竟只有一个操作员!因此,它不会与其他任何东西争夺优先权.看起来很简单.我弄错了什么???
&&
优先级and
高,优先级低.
@buddyship.involve? current_user && @buddyship.update(buddyship_params)
Run Code Online (Sandbox Code Playgroud)
是相同的
@buddyship.involve?(current_user && @buddyship.update(buddyship_params))
Run Code Online (Sandbox Code Playgroud)
和
@buddyship.involve? current_user and @buddyship.update(buddyship_params)
Run Code Online (Sandbox Code Playgroud)
是相同的
@buddyship.involve?(current_user) and @buddyship.update(buddyship_params)
Run Code Online (Sandbox Code Playgroud)