ruby - Rails 3.2 model constructor with Rails 4 -


my model constructor:

3.2:

  def initialize(attributes = {})     super # must allow active record initialize!     attributes.each |name, value|       send("#{name}=", value)     end   end 

4.0.4:

activemodel::forbiddenattributeserror

how change 3.2 constructor 4.0.4 compatibility?

i guess getting error when try create new user user controller?

if have code like:

class userscontroller < applicationcontroller   ...   def create     @user = user.new(params[:user])     ...   end   ... end 

then won't work because can no longer mass assignments in rails 4.

instead need whitelist parameters in controller this:

class userscontroller < applicationcontroller   ...   def create     @user = user.new(user_params)     ...   end   ...    private    def user_params     params.require(:user).permit(:username, :password, :password_confirmation, :email)   # etc. according user model   end end 

having said this, i'm not sure why need constructor in model @ all? initialize method inheriting activerecord::base should enough. in event will need whitelist parameters in controller avoid forbiddenattributeserror error.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -