简体   繁体   English

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

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

I am having some issues correctly formatting the code below following Rubocop inspection in my Ruby on Rails Application. 在我的Ruby on Rails应用程序中检查Rubocop之后,我在正确格式化下面的代码时遇到一些问题。 I keep getting the errors: 我不断收到错误:

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

and

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

I just can't figure out what the issue is with the code and how to fix it. 我只是无法弄清楚代码的问题是什么以及如何解决它。

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

You are not using this [:success, JSON.parse(response.to_str)] and this [:unprocessable_entity, JSON.parse(response.to_str)] anywhere in your code. 您不在代码中的任何地方使用此[:success, JSON.parse(response.to_str)]和此[:unprocessable_entity, JSON.parse(response.to_str)] Either remove it or assign it in a variable but assigning it in a variable and not using that variable may cause the warning again. 删除它或将其分配给变量,但将其分配给变量而不使用该变量可能会再次引起警告。

You should remove the code which you are not using. 您应该删除不使用的代码。 Try this: 尝试这个:

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

or try this: 或尝试以下方法:

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

Let me know if it worked. 让我知道它是否有效。

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

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