2016-09-18 2 views
0

私は、2つのテーブル、すなわち、ユーザテーブルと、スクールユーザーと呼ばれるテーブルを持っています。 School_Usersテーブルは、電子メールを含むユーザーのロールやその他のさまざまなフィールドを処理します。RoR:Deviseとログインしてメールを委任する

user.rb

delegate :email, to: :school_user 

そして私は、アクティブな管理を使用して、そこにそれをテストしています、仕事に表示されます。

私は、このモデルに電子メールを委任しました。

しかし、私はアプリケーションにサインインする行くとき、私はこのエラーを取得:

User#email delegated to school_user.email, but school_user is nil: #<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, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: nil, updated_at: nil, admin: false, first_name: nil, last_name: nil> 

私はここで、これは単なるビューであると思いますが、私のフォームは、電子メールのフィールドである:

/セッション/新.html.erb

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 

       <p class="loginLogo"><%= image_tag "logo.png", class: "text-center" %></p> 
       <%= f.label :email, :class => "text-center font16" %> 
       <span class="glyphicon glyphicon-envelope"></span> 
       <%= f.email_field :email, :class => "insdat", :placeholder => "Email Address", autofocus: true %> 
       <%= f.label :password, :class => "text-center font16" %> 
       <span class="glyphicon glyphicon-lock"></span><%= f.password_field :password, :class => "insdat" , :placeholder => "Password", autocomplete: "off" %> 
       <div class="actions"> 
      <%= f.submit "Log in", :class => "expanded button logbtn" %> 
    </div> 
    <div class="text-center"> 

<% end %> 

フォームを読み込む方法を教えてください。私はそれを取る私はschool_userに向かってフィールドを定義するか、またはパスを見つける必要がありますか?

ありがとうございました。必要に応じてより多くのコードを投稿してください。

答えて

1

使用されているUserのインスタンスにschool_userの値がnilであるという問題があります。

あなたは、修正することができるとnilのを可能にすることによって:

delegate :email, to: :school_user, allow_nil: true 
+0

こんにちは、おかげで、ちょうどこれはあなたの意見のいずれかの欠点を持っていない不思議?ありがとう。私はDeviseを使っています。 – Co2