Ruby on rails Rubocop:在void上下文中使用的文本。[Lint/void]

Ruby on rails Rubocop:在void上下文中使用的文本。[Lint/void],ruby-on-rails,rubocop,Ruby On Rails,Rubocop,在我的RubyonRails应用程序中进行Rubocop检查之后,我在正确格式化下面的代码方面遇到了一些问题。我不断发现错误: Rubocop:Literal[:success,JSON.parse(response.to_str)]在void上下文中使用。[Lint/void] 及 Rubocop:Literal[:不可处理的_实体,JSON.parse(response.to_str)]在void上下文中使用。[Lint/void] 我只是不知道代码有什么问题,以及如何修复它 case re

在我的RubyonRails应用程序中进行Rubocop检查之后,我在正确格式化下面的代码方面遇到了一些问题。我不断发现错误:

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

Rubocop:Literal[:不可处理的_实体,JSON.parse(response.to_str)]在void上下文中使用。[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
让我知道它是否有效