Rails 4 - undefined method build in using strong parameters

145 views
Skip to first unread message

Erwin

unread,
Sep 17, 2013, 1:27:24 PM9/17/13
to rubyonra...@googlegroups.com
In my backoffice/users_controller.rb , I wrote a class to build the permitted parameters

class Backoffice::UsersController < ApplicationController

    class UserParams
        def build params
            params.require(:user).permit(:email, :password, :password_confirmation )
        end
      end

but using it in the create method,
  def create
    @user = User.new(UserParams.build(params))

 I get an error : 

undefined method `build' for Backoffice::UsersController::UserParams:Class


 I guess I am wrong somewhere as the class is defined in the Backoffice module... where I am wrong ?


thanks for help

Colin Law

unread,
Sep 17, 2013, 1:31:43 PM9/17/13
to rubyonra...@googlegroups.com
On 17 September 2013 18:27, Erwin <yves_...@mac.com> wrote:
> In my backoffice/users_controller.rb , I wrote a class to build the
> permitted parameters
>
> class Backoffice::UsersController < ApplicationController
>
> class UserParams
> def build params

If you want a class method (rather than instance) this should be
def self.build( params )

> params.require(:user).permit(:email, :password,
> :password_confirmation )
> end
> end
>
> but using it in the create method,
> def create
> @user = User.new(UserParams.build(params))
>
> I get an error :
>
> undefined method `build' for Backoffice::UsersController::UserParams:Class

Because you have defined an instance method not a class method

Colin

Erwin

unread,
Sep 18, 2013, 1:58:52 AM9/18/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
Thanks Colin...  changed to :

  private
 
  def user_params

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

and cretae /update...
  def create
    @user = User.new(user_params)
Reply all
Reply to author
Forward
0 new messages