[英]Creating Profile for Devise users
So I took the steps described in Profile model for Devise users? 所以我采用了Profile模型中为Devise用户描述的步骤? I'm using rails 4 and ruby 1.9.3p448
我正在使用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
#
<h2>Sign up...</h2> <%= form_for resource, :as => resource_name, :url => registration_path(resource_name) do |f| %> <%= devise_error_messages! %> <div><%= f.label :email %><br /> <%= f.email_field :email, :autofocus => true %></div> <div><%= f.label :password %><br /> <%= f.password_field :password %></div> <div><%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation %></div> <%= f.fields_for :profile do |profile_form| %> <h2><%= profile_form.label :uname %></h2> <p><%= profile_form.text_field :uname %></p> <h2><%= profile_form.label :manager %></h2> <p><%= profile_form.text_field :manager %></p> <% end %> <div><%= f.submit "Sign up" %></div> <% end %> <%= render "devise/shared/links" %>
#
Still can't save profile for user and this is the output: 仍无法为用户保存配置文件,这是输出:
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**
Shoud I do anything in Profiles controller? 我在Profiles控制器中做了什么? Thanks in advance
提前致谢
I found the solution! 我找到了解决方案! In my users/registrations_controller.rb, I had to add the following
在我的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
and it works just fine! 它工作得很好!
Rails 4 has stopped using attr_accessible and has started using strong parameters - http://api.rubyonrails.org/classes/ActionController/StrongParameters.html Rails 4已停止使用attr_accessible并已开始使用强参数 - http://api.rubyonrails.org/classes/ActionController/StrongParameters.html
You'll need to add your nested attributes for profile into the permitted attributes in the user controller. 您需要将配置文件的嵌套属性添加到用户控制器中的允许属性中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.