简体   繁体   English

rails 4中的attr_accessible

[英]attr_accessible in rails 4

I'm doing the onemonth rails and I'v got a problem with the attr_accessible function. 我正在做一个月的轨道,我遇到了attr_accessible函数的问题。 I've installed it as a gem in rails 4(gem 'protected_attributes') and using it with the simple_form. 我已将它作为gem 4(gem'en protected_attributes')中的gem安装,并将其与simple_form一起使用。

But the problem is that when I update my form with a name, it doesnt remember it! 但问题是,当我用名字更新我的表单时,它不会记住它! But it says it updated successfully?? 但它说它成功更新??

Ths is my user.rb 这是我的user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :name
end

Since you are using Devise you can remove the entire attr_accessible line (and the strong_parameters gem, see more below). 由于您使用的是Devise ,因此可以删除整个attr_accessible行(以及strong_parameters gem,请参阅下面的内容)。 Devise provides a controller which handles sign-up for you already. Devise提供了一个控制器 ,可以为您处理注册。

If you want to add other attributes to your user you can subclass Devise::RegistrationsController with your custom controller: 如果要向用户添加其他属性,可以使用自定义控制器将Devise::RegistrationsController子类化:

# app/controllers/registrations_controller
class RegistrationsController < Devise::RegistrationsController

  private

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

  def account_update_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation, :current_password)
  end
end

You then need to tell Devise to route to your custom controller: 然后,您需要告诉Devise路由到您的自定义控制器:

# config/routes.rb
devise_for :users, :controllers => { registrations: 'registrations' }

PS . PS I would also recommend removing the strong_parameters gem and use the out of the box Rails 4 strong parameters . 我还建议删除strong_parameters gem并使用开箱即用的Rails 4强参数 There are several known problems with whitelisting parameters on the model level (different params for different actions for example). 模型级别的白名单参数存在几个已知问题(例如,不同操作的不同参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM