为Devise用户创建配置文件
所以我采用了Profile模型中为Devise用户描述的步骤? 我正在使用rails 4和ruby 1.9.3p448
class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes has_one :profile accepts_nested_attributes_for :profile protected def profile super || build_profile end end
#
class Profile < ActiveRecord::Base belongs_to :user attr_accessible :uname, :manager end
#
Sign up...
resource_name, :url => registration_path(resource_name) do |f| %>
true %>
#
仍无法为用户保存配置文件,这是输出:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kG7S9lF4+5hm+ggmKA4LZyXrN4hsPf01jGQvKxgzGGI=", "user"=>{"email"=>"test1@test.test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "profile_attributes"=>{"uname"=>"1", "manager"=>"1"}}, "commit"=>"Sign up"} **Unpermitted parameters: profile_attributes**
我在Profiles控制器中做了什么? 提前致谢
我找到了解决方案! 在我的users / registrations_controller.rb中,我不得不添加以下内容
before_filter :configure_permitted_parameters, if: :devise_controller? def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :password, :password_confirmation, :remember_me, profile_attributes: [:uname, :manager])} end
它工作得很好!
Rails 4已停止使用attr_accessible并已开始使用强参数 – http://api.rubyonrails.org/classes/ActionController/StrongParameters.html
您需要将配置文件的嵌套属性添加到用户控制器中的允许属性中。
生成正常的Users_controller包括show
, edit
, index
,甚至create
和new
操作,如果需要!,并在路径文件中使用它
devise_for :users, :controllers => { :registrations => "users" } resources :users, only: [:index, :new, :create, :show, :destroy, :edit, :update]
如你所见,这将覆盖设计的注册控制器,并允许你使用自己的用户控制器,如果你不喜欢使用设计所做的,你可以创建自己的注册表和编辑表。 并且不要忘记破坏Profile模型,因为如果你按照我的回答你不需要它。
- 在Rails 4表单中将列表作为参数数组提交,使用javascript将params值添加到params hash
- Rails:使用where子句查找深层嵌套的关联
- 是否可以在rake任务中包含模块并使其方法可用于rails app中的任务?
- 升级到Rails 3后,为什么所有字符串都是ASCII-8BIT?
- Rails复杂has_many和has_many通过
- Shoulda Matcher和has_many通过:未定义的方法`class_name’为nil:NilClass
- 使用rails 2.3.18在(Gem :: LoadError)中找不到bundler(> = 0)
- Rspec测试失败,当用户被授权访问他拥有的交易时,用户被重定向
- 模块内部控制器的规范(版本主义者)