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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d0de8d7d-b68e-4ba8-9976-302b10a09298%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Sep-17 17:31 UTC
Re: Rails 4 - undefined method build in using strong parameters
On 17 September 2013 18:27, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> In my backoffice/users_controller.rb , I wrote a class to build the > permitted parameters > > class Backoffice::UsersController < ApplicationController > > class UserParams > def build paramsIf 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:ClassBecause you have defined an instance method not a class method Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuPhNwBZ04CFQdYvDjRxTrpJQJfrBwXnSzOECtPdQgoBA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Erwin
2013-Sep-18 05:58 UTC
Re: Rails 4 - undefined method build in using strong parameters
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) Le mardi 17 septembre 2013 19:31:43 UTC+2, Colin Law a écrit :> > On 17 September 2013 18:27, Erwin <yves_...-ee4meeAH724@public.gmane.org <javascript:>> 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d856d60b-61a3-4b84-b347-1f112be8c5db%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.