0

我正在建立一個工作板,因此有兩個用戶模型,僱主和申請人。 我正在使用設計,因此我現在面臨的挑戰是與多態關係一起使用。使用設計時的多態用戶模型

通過一些rails控制檯測試,我知道devise中的(資源)傳遞所有用戶信息,並且它傳遞了申請人或僱主的role_type。然而,它被設置爲零,因此儘管我的用戶正在被保存,但它並沒有保存申請人或僱主的角色類型。此外,用戶表中的申請人或僱主表中沒有任何內容。我的代碼是在下面,但我的問題真的是如何將角色類型傳遞到設計資源哈希?或者如果這不可能是解決這個問題的最優雅的方式。

非常感謝!

而且整個項目代碼是在這裏如果我錯過了什麼https://github.com/PatGW/jobs

下面是則須─> devise->用戶> new.html.erb

<h1>Sign Up</h1> 

<%= form_for @user do |f| %> 
<% if @user.errors.any? %> 
    <div class="error_messages"> 
     <h2> Form is invalid</h2> 
     <ul> 
      <% for message in @user.errors.full_messages %> 
      <li><%= message %></li> 
      <% end %> 
     </ul> 
    </div> 
<% end %> 
<p> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
</p> 
<p> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
</p> 
<p> 
    <%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %> 
</p> 

<p> 
    <%= radio_button_tag :role_type, "employer", :checked => false %><br /> 
    <%= label :role_type, 'Employer' %> 
</p> 
<p> 
    <%= radio_button_tag :role_type, "applicant", :checked => true %><br /> 
    <%= label :role_type, 'Applicant' %> 
</p> 


<p class="button"><%= f.submit %></p> 
<% end %> 

繼承自DeviseController的UsersController

class UsersController < Devise::RegistrationsController 

def new 
super 
end 

def create 
User.transaction do 
    super 
    after_sign_in_path(resource) 
end 
end 

private 

def after_sign_in_path(resource) 
debugger 
    @user = User.new(params[:user]) 
if params[:role_type] == "coach" 
    role = Employer.create 
else params[:role_type] == "player" 
    role = Applicant.create 
end 
end 


end 

回答

0

這裏的問題不是Devise相關的,而是關聯/ OO相關的。我檢查了您的代碼:您的申請人和僱主模型似乎有一個用戶字段,而這些模型實際上應該從用戶繼承,因爲他們是一種用戶。所以,試試這個:

class Employer < User 
    attr_accessible :location, :logo, :name 
end 

對於初學者,看看它是否有幫助。 (順便說一句:你也應該更改申請人的代碼)

+0

嗨Stobbej,給了一個嘗試,但它沒有幫助。我有一個使用rails調試器的玩法,我可以看到role_type參數沒有在設計資源中傳遞,也沒有在調用create方法時保存。有點難倒任何想法? – PatGW

相關問題