Search code examples
ruby-on-railsrubydevisejwtdevise-token-auth

NameError (undefined local variable or method `sign_up_params' for #<RegistrationsController:0x0000000000f280>)


I have this error, it occurs when I try to POST the user, I can't understand, pls help me I don't know what else to do, I didn't find anything before doing this post, tks. My files:

registrations_controller.rb

class RegistrationsController < ApplicationController
  respond_to :json
  
  def create
    build_resource(sign_up_params)
    
    resource.save
    
    if resource.errors.empty?
      render json: resource
    else
      render json: resource.errors
    end
  end
end

sessions_controller.rb

class SessionsController < ApplicationController
  respond_to :json
  
  private
  
  def respond_with(resource, _opts = {})
  render json: resource
  end

  def respond_to_on_destroy
    head :no_content
  end
end

users_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :update, :destroy]

  # GET /users
  def index
    @users = User.all

    render json: @users
  end

  # GET /users/1
  def show
    render json: @user
  end

  # POST /users
  def create
    @user = User.new(user_params)

    if @user.save
      render json: @user, status: :created, location: @user
    else
      render json: @user.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /users/1
  def update
    if @user.update(user_params)
      render json: @user
    else
      render json: @user.errors, status: :unprocessable_entity
    end
  end

  # DELETE /users/1
  def destroy
    @user.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def user_params
      params.require(:user).permit(:email, :password)
    end
end

model/user.rb

class User < ApplicationRecord
  include Devise::JWT::RevocationStrategies::JTIMatcher
  
  devise :registerable,
  :database_authenticatable,
  :jwt_authenticatable,
  jwt_revocation_strategy: self
  
end

routes.rb

    Rails.application.routes.draw do
      devise_for :users,
        path: '',
        path_names: {
        sign_in: 'login',
        sign_out: 'logout',
        registration: 'signup'
      },
      controllers: {
      sessions: 'sessions',
      registrations: 'registrations'
      }
end

migrate/create_user.rb

class CreateUsers < ActiveRecord::Migration[6.1]
  def change
    create_table :users do |t|
      t.string :email
      t.string :password
      t.string :jti, null: false, default: ""

      t.timestamps
    end
  end
end

migrate/add_devise_to_users.rb

class AddDeviseToUsers < ActiveRecord::Migration[6.1]
  def self.up
    change_table :users do |t|
      ## Database authenticatable
     # t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
    # t.string   :reset_password_token
    #  t.datetime :reset_password_sent_at

      ## Rememberable
      # t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.string   :current_sign_in_ip
      # t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at
      # t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    # add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end

  def self.down
    raise ActiveRecord::IrreversibleMigration
  end
end

schema.rb

ActiveRecord::Schema.define(version: 2021_03_19_040209) do
  create_table "users", force: :cascade do |t|
    t.string "email"
    t.string "password"
    t.string "jti", default: "", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "encrypted_password", default: "", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
  end
end

Gemfile

source 'https://rubygems.org'

ruby '2.7.2'

gem 'rails', '~> 6.1.3'
gem 'sqlite3', '~> 1.4'
gem 'puma', '~> 5.0'
gem 'bootsnap', '>= 1.4.4', require: false

group :development, :test do
  gem 'devise'
  gem 'devise-jwt'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'listen', '~> 3.3'
  gem 'spring'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Solution

  • The RegistrationsController should inherit from Devise::RegistrationsController since you're trying to override it. Once you do that, you can access the sign_up_params instance method.

    class RegistrationsController < Devise::RegistrationsController
      # ...
    end