Im bị kẹt nghiêm trọng về vấn đề mà tôi có mô hình Người dùng và Hồ sơ. bất kỳ điều gì tôi thử có vẻ như xác thực không được kích hoạt cho bất kỳ thuộc tính Tiểu sử nàobị kẹt nghiêm trọng: các xác thực không được kích hoạt cho tài nguyên lồng nhau với fields_for
- Mô hình hồ sơ thuộc về Người dùng. Tài has_one Hồ sơ
- On mẫu đăng ký sử dụng fields_for có một số lĩnh vực thông tin hiển thị (giới tính, và thành phố và quốc gia)
- Dù tôi đã cố gắng, tôi không thể để cho việc xác nhận cho giới và thành phố (hồ sơ cá nhân fields_for lĩnh vực)
hình thức của tôi:
- 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) + " »"
mô hình tài khoản của tôi có này:
accepts_nested_attributes_for :profile
Thông tin cá nhân mô hình với kiểm chứng thực là như:
validates_presence_of :gender
validates_presence_of :city
validates_presence_of :country
params của tôi:
-- !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
Rails console không:
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>
Profile.new
=> #<Profile id: nil, user_id: nil, gender: nil, country: nil, state: nil, city: nil, status: nil, counter_cache: nil>
- CẬP NHẬT 19 Tháng 7 '12
tôi thêm
- @user.errors.full_messages.each do |message|
= message
.clear
Để hình thức để xem chính xác những gì đang xảy ra và điều này ném sau danh sách lỗi:
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
Vì vậy, có vẻ như xác thực được kiểm tra .... Nhưng không có kiểu dáng lỗi được áp dụng! Ngoài ra hồ sơ không hợp lệ ???? Tôi không chắc chắn nhưng có nghĩa là hãy tham khảo
Hãy thử thay đổi thành 'validates_inclusion_of: giới tính, bằng:% w (nam nữ)' và xem liệu nó có cung cấp cho bạn thông báo lỗi –
Thx đã cố gắng vô ích không. Tôi thử nó với một số lĩnh vực hồ sơ khác tôi sẽ thêm vào các hình thức có lẽ là một vấn đề liên quan đến giới tính của mình – Rubytastic
Bạn đã thử đặt 'validates_associated: profile' trong mô hình' Người dùng' của bạn chưa? – HargrimmTheBleak