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

My HTML document is not linking to my CSS stylesheet properly -

php array slice every 2th rule -

node.js - Sending sockets to client side, Error: Converting circular structure to JSON -