InstanceVariableAssumption: UsersController assumes too much for instance variable '@user'

Viewed 2021

I'm following Michael Hartl tutorial Rails course. On chapter 7, I run Reek on UsersController and I got this following warning:

app/controllers/users_controller.rb -- 1 warning:
1:InstanceVariableAssumption: UsersController assumes too much for instance variable '@user' [https://github.com/troessner/reek/blob/master/docs/Instance-Variable-Assumption.md]

Here is my code:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new user_params
    if @user.save
      flash[:success] = t "welcome_to_app"
      redirect_to @user
    else
      render "new"
    end
  end

  def show
    @user = User.find_by id: params[:id]

    return if @user
    flash[:danger] = t "not_exist_user"
    redirect_to root_path
  end

  private

  def user_params
    params.require(:user).permit :name, :email, :password,
      :password_confirmation
  end
end

Kindly explain why I got this error InstanceVariableAssumption and how to fix this.

3 Answers
Related