Rails 4和Devise:嵌套属性验证不起作用

时间:2015-07-16 14:14:57

标签: ruby-on-rails ruby devise

我在我的rails 4 app中使用Devise。我有一个成员模型(用于设计)和一个包含每个成员的所有配置文件信息的用户模型。成员接受用户的嵌套属性。请参阅下面的表单,了解我的routes.rb文件中的新注册以及自定义路由。

我的问题是这样 - 除了验证用户属性外,一切正常。如果我将名字和姓氏留空,那么它会验证(但崩溃如下:

enter image description here

但是对于Member(设计模型)的所有其他验证都有效 - 它们最终会出现在/ members页面上,并显示错误消息。我不确定发生了什么 - 不应该设计显示无效嵌套属性的错误消息?

class Members::RegistrationsController < Devise::RegistrationsController

    # GET /resource/sign_up
     def new
        build_resource({})
        self.resource.user = User.new
        respond_with self.resource
     end

    # POST /resource
    def create
        super
        resource.user.ip_address = request.remote_ip
        resource.user.email = resource.email
        resource.user.save!
    end

    private

    def sign_up_params
        allow = [:provider, :uid, :email, :password, :password_confirmation, user_attributes: [:member_id, :email, :first_name, :last_name, :institution, :city, :country, :job_title, :about, :tag_list, :picture, :ip_address]]
        params.require(resource_name).permit(allow)
    end

  end

Member.rb

  class Member < ActiveRecord::Base
      devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :trackable

      has_one :user, dependent: :destroy, autosave: true

      accepts_nested_attributes_for :user

  end

User.rb

  class User < ActiveRecord::Base

       belongs_to :member

       validates :first_name, presence: true

       validates :last_name, presence: true

  end

在我的routes.rb

   devise_for :members, controllers: {registrations: 'members/registrations',
                                      omniauth_callbacks: 'members/omniauth_callbacks',
                                      sessions: 'members/sessions' }

在我的会员/ registrations / new.html.erb

       <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { role: "form" }) do |f| %>

          <%= f.fields_for(:user) do |user_fields| %>

          <%= user_fields.text_field :first_name, autofocus: true, class: 'form-control', :placeholder => "First name" %>

          <%= user_fields.text_field :last_name, class: 'form-control', :placeholder => "Last name" %>

           <% end %>

       <%= f.email_field :email, autofocus: true, class: "form-control", :placeholder => "Email" %>

       <%= f.password_field :password, class: "form-control", :placeholder => "Password" %>

       <%= f.submit t('.sign_up', :default => "Sign up"), class: "btn btn-danger" %>

       <% end %>

以下是此

的日志
   Started POST "/members" for ::1 at 2015-07-16 15:18:54 +0100
   Processing by Members::RegistrationsController#create as HTML
   Parameters: {"utf8"=>"✓",          "authenticity_token"=>"sIxK1PTvrxNHUBdDXAtKFLLG27FvqypQ3mUvo398tHOMomi1S3I2o3vBM GwrF6PVN1NFBR5vnr8ezKP6XnMzqw==", "member"=>{"user_attributes"=>{"first_name"=>"", "last_name"=>""}, "email"=>"slfwalsh@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.2ms)  begin transaction
   Member Exists (0.3ms)  SELECT  1 AS one FROM "members" WHERE "members"."email" = 'slfwalsh@gmail.com' LIMIT 1
  (0.1ms)  rollback transaction
  Rendered devise/shared/_links.erb (9.9ms)
    Rendered members/registrations/new.html.erb within layouts/static_home_page (19.8ms)
    Rendered layouts/_static_login_header.html.erb (4.1ms)
     (0.1ms)  begin transaction
    (0.1ms)  rollback transaction
    Completed 422 Unprocessable Entity in 1499ms

1 个答案:

答案 0 :(得分:2)

您看到该页面的原因是save!触发 验证错误 ,如果任何属性的验证失败并显示这些错误在页面中。另一方面,save 没有 显示 验证错误 ,但只是取消保存事务 。更改下面的创建方法可以解决您的问题。

def create
  super
  resource.user.ip_address = request.remote_ip
  resource.user.email = resource.email
  resource.user.save
end