Home  >  Q&A  >  body text

ruby - 虚拟属性的 属性名= 方法不执行

user.rb 定义了 password 虚拟属性

user.html.erb 定义了虚拟属性表单

然而输出了一下 password= 那个方法没有执行....23333

请指教

巴扎黑巴扎黑2710 days ago675

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-24 09:11:50

    Because ror 4.2 is used, the correct way is to add your virtual attributes to :permit(:virtual attribute name)

    reply
    0
  • 阿神

    阿神2017-04-24 09:11:50

    I haven’t used erb much, but are you sure that this method will be called in the template? password= I am used to using
    callbacks for this kind of needs...before_create

    Also, leave this kind of thing to professionals.

    reply
    0
  • 怪我咯

    怪我咯2017-04-24 09:11:50

    Ruby on Rails does not need to write attr_accessor directly.

    But you need to use strong-parameters. Refer to the following writing method and add password to the whitelist:
    http://edgeguides.rubyonrails.org/action_controller_overview.html#stro...

    reply
    0
  • Cancelreply