0

I´m new to Rails and I´ve been stuck trying to set the sign up on my rails application using devise gem. When I create the user the following error pops up:

ArgumentError in UsersController#create too few arguments

enter image description here

but the user still is created succesfully and added into the db.

This is my routes.rb file:

  resources :users
  devise_for :users
  root to: 'static_pages#home'
  get '/signup', to: 'users#new' 

The create function in the users_controller.rb is:

    @user = User.new(user_params)
      if @user.save
        format.html { redirect_to user_session_url(@user), notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        render 'new'
      end
    end 

So far I noticed that when I swap the lines of resources :users with devise_for :users, it doesn´t throw me the error, but when listing all users it has only been saved the mails and password required by devise and not the other data of the user as the name, last name, etc.

This is my User.rb file:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  validates :name, presence: true, length: {minimum:2, maximum:20}
  validates :last_name, presence: true, length: {minimum:2, maximum:20}
end
Milan
  • 75
  • 1
  • 4
  • 1
    What line the error happens in? Also, please post the whole error message. – Aleksei Matiushkin Apr 27 '19 at 04:53
  • I just edited and uploaded the picture. Thanks! – Milan Apr 27 '19 at 04:56
  • Possible duplicate of https://stackoverflow.com/questions/11209900/too-few-arguments. Have you tried with `respond_to do .... end`. Reference: https://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to – Tenzin Chemi Apr 27 '19 at 08:01

2 Answers2

1

First of all, when we write

devise_for :users

It creates predefined routes for you that includes sign_up and sign_in path. if you want to save extra parameters then "sanitize" them. For example:

class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
   before_action :configure_permitted_parameters, if: :devise_controller?

   protected
    def configure_permitted_parameters
       devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :lastname])
    end
 end

If you want to make own custom controller and routes then skip the predefined routes like:

 devise_for :users, :skip => [:sessions, :passwords, :registrations]

and make it you own in routes.rb file:

devise_for :users, :skip => [:sessions, :passwords, :registrations]

namespace :api, defaults: {format: :json} do
    devise_for :users, :controllers => {
         :sessions => "api/sessions",
         :passwords => "api/passwords"
    }
end

And session_controller.rb

def session_params
    params.require(:session).permit(:name, :last_name, :all_extra_parameters_defined_here)
end

In user_controller ,No need to use format.html , just redirect it.

Pragya Sriharsh
  • 529
  • 3
  • 6
0

your user_params should contain:

     def user_params
          params.require(:user).permit(:name, :email, :last_name, 
          :admin, :password, 
       :remember_me)
    end

you did not show that part.

Nsikan Sylvester
  • 583
  • 5
  • 14