Rub*_*tic 8 validation activerecord ruby-on-rails nested-attributes ruby-on-rails-3
我严重关注我有用户和个人资料模型的问题.无论我尝试什么,似乎都没有触发任何Profile属性的验证
我的表格:
- title t(:title, :scope => :register)
%h1= yield(:title)
= simple_form_for(resource, :as => resource_name, :html => { :class => 'form-horizontal' } , :validate => true , :url => registration_path(resource_name)) do |f|
= f.input :username, :label => t(:username)
= f.input :email, :label => t(:email),
:hint => t(:hint_email_visible)
= f.input :password, :label => t(:password), :require => true
= f.input :password_confirmation, :label => t(:password_confirm)
- resource.build_profile
= f.fields_for :profile do |f|
#div
= f.hidden_field :form, :value => "signup"
.clear
= f.input :gender, :collection => [['Male', 'male'], ['Female', 'female']],
:as => :radio
= f.input :city,
:readonly => "readonly",
:label => t(:city)
.ruler
= f.input :country,
:label => "Your country",
:collection => [DataCountry.where(:code => 155).first],
:value => @city,
:id => "country",
:name => "country"
.clear
= f.button :submit, t(:submit, :scope => :register) + " »"
Run Code Online (Sandbox Code Playgroud)
我的用户模型有:
accepts_nested_attributes_for :profile
Run Code Online (Sandbox Code Playgroud)
带有验证的我的个人资料模型如下:
validates_presence_of :gender
validates_presence_of :city
validates_presence_of :country
Run Code Online (Sandbox Code Playgroud)
我的参数:
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ?
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
invite_code: ''
username: username
email: ''
password: ''
password_confirmation: ''
profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
form: signup
gender: male
dob(3i): '9'
dob(2i): '7'
dob(1i): '1942'
city: 'somevalue'
country: 'somevalue'
commit: Register »
action: create
controller: registrations
Run Code Online (Sandbox Code Playgroud)
Rails控制台做:
User.new
=> #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, password_salt: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, authentication_token: nil, username: nil, is_blocked: nil, is_deleted: nil, role: nil, slug: nil, created_at: nil, updated_at: nil, last_seen: nil, credits: nil, invite_code: nil, is_moderated: nil, locale: nil, status: nil, login_token: nil>
Run Code Online (Sandbox Code Playgroud)
Profile.new
=> #<Profile id: nil, user_id: nil, gender: nil, country: nil, state: nil, city: nil, status: nil, counter_cache: nil>
Run Code Online (Sandbox Code Playgroud)
- 更新于2012年7月19日
我加了
- @user.errors.full_messages.each do |message|
= message
.clear
Run Code Online (Sandbox Code Playgroud)
到表单中查看到底发生了什么,这会引发以下错误列表:
Email can't be blank
Email can't be blank
Password can't be blank
Password can't be blank
Profile city can't be blank
Profile gender can't be blank
Profile is invalid
Username can't be blank
Password confirmation can't be blank
Run Code Online (Sandbox Code Playgroud)
所以似乎检查了验证....但是没有应用错误样式!该个人资料也无效???? 我不确定,但这是指的
现在花了很多时间尝试解决这个问题,根据本主题中的建议,解决方案非常简单,我发现而不是
错误的:
- resource.build_profile
Run Code Online (Sandbox Code Playgroud)
正确的:
- resource.build_profile unless resource.profile
Run Code Online (Sandbox Code Playgroud)
这样做的作用不是清理配置文件,而是使用旧的配置文件(如果可用)...所以不会清除所有错误!希望这对将来的人有帮助!
| 归档时间: |
|
| 查看次数: |
983 次 |
| 最近记录: |