繁体   English   中英

Rubocop:在无效上下文中使用的文字。[Lint / Void]

[英]Rubocop: Literal used in void context.[Lint/Void]

在我的Ruby on Rails应用程序中检查Rubocop之后,我在正确格式化下面的代码时遇到一些问题。 我不断收到错误:

在无效上下文中使用的Rubocop:Literal [:success,JSON.parse(response.to_str)]。[Lint / Void]

在无效上下文中使用的Rubocop:Literal [:unprocessable_entity,JSON.parse(response.to_str)]。[Lint / Void]。

我只是无法弄清楚代码的问题是什么以及如何解决它。

case response.code
when 200
  [:success, JSON.parse(response.to_str)]
  redirect_to root_url, notice: 'Logged in!'
when 422
  [:unprocessable_entity, JSON.parse(response.to_str)]
  flash.now[:alert] = 'Email or password is invalid'
  render 'new'
else
  raise "Invalid response #{response.to_str} received."
end

您不在代码中的任何地方使用此[:success, JSON.parse(response.to_str)]和此[:unprocessable_entity, JSON.parse(response.to_str)] 删除它或将其分配给变量,但将其分配给变量而不使用该变量可能会再次引起警告。

您应该删除不使用的代码。 尝试这个:

case response.code
when 200
  redirect_to root_url, notice: 'Logged in!'
when 422
  flash.now[:alert] = 'Email or password is invalid'
  render 'new'
else
  raise "Invalid response #{response.to_str} received."
end

或尝试以下方法:

case response.code
when 200
  @success = [:success, JSON.parse(response.to_str)]
  redirect_to root_url, notice: 'Logged in!'
when 422
  @error = [:unprocessable_entity, JSON.parse(response.to_str)]
  flash.now[:alert] = 'Email or password is invalid'
  render 'new'
else
  raise "Invalid response #{response.to_str} received."
end

让我知道它是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM