0

I'm trying to implement simple authorization following by certain tutorial.

class AuthController < ApplicationController

    layout 'public'
    def auth_user
        user = User.authenticate(login_params)
        if user
            session[:user_id] = user.id
            redirect_to(:action => 'home')
        else
            flash[:notice] = "wrong username or password"
            flash[:color]= "invalid"
            render "login"  
        end
    end

  private

  def login_params
    params.require(:login_data, :password)
  end

end

and getting the exception at params.require saying 'wrong number of arguments (2 for 1)'. can't understand, what am I doing wrong? rails 4.1.1

heximal
  • 10,327
  • 5
  • 46
  • 69

1 Answers1

2

The require method, or more specifically ActionController::Parameters#require, only takes a param key as argument.

As you can see from the Rails 4.1.1 source code:

# File actionpack/lib/action_controller/metal/strong_parameters.rb, line 172
def require(key)
  self[key].presence || raise(ParameterMissing.new(key))
end
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
  • was it modified since rails 4.0.0? cause this is the only explanation why it works with the tutorial I'm using – heximal Jun 30 '14 at 21:28
  • I don't think so. The method looked very similar on the 4.0 stable branch: https://github.com/rails/rails/blob/4-0-stable/actionpack/lib/action_controller/metal/strong_parameters.rb#L182-L189 – Marcelo De Polli Jun 30 '14 at 21:48
  • Are you sure your tutorial is not using `#permit` with two arguments rather than `#require`? – Marcelo De Polli Jun 30 '14 at 21:49
  • I can't find the url for tutorial, but yes, I'm sure. here it is `private def admin_user_params params.require(:admin_user).permit(:first_name, :last_name, :email, :username, :password) end` – heximal Jun 30 '14 at 21:55
  • 1
    There you go. It's `#permit` that's taking several arguments in the snippet you posted, not `#require`. – Marcelo De Polli Jun 30 '14 at 21:57
  • ah, you're right, my shame. everywhere require is mentioned in this tutorial it accepts only one argument. I don't see the point of this then. what if I want more required params than one? – heximal Jun 30 '14 at 21:58