Note that there are some explanatory texts on larger screens.

plurals
  1. POPolymorphic user model when using devise
    primarykey
    data
    text
    <p>I am building a job board and therefore have two user models and employer and an applicant. I am using devise and therefore my challenge now is to use this with the polymorphic relationship.</p> <p>Through some rails console testing I know that (resource) in devise passes all the user information and it is passing the role_type which is either applicant or employer. However it is set to nil and therefore although my user is being saved it is not saving the role type of applicant or employer. Furthermore nothing is being saved in the applicant or employer tables just in the user table. My code is below but my question really is how do I pass the role_type to the devise resource hash? Or if this isnt possible the most elegant way of solving this problem.</p> <p>Many Thanks!</p> <p>Also the full project code is here if I have missed anything <a href="https://github.com/PatGW/jobs" rel="nofollow">https://github.com/PatGW/jobs</a></p> <p>Below is the views->devise->user->new.html.erb</p> <p></p> <pre><code>&lt;h1&gt;Sign Up&lt;/h1&gt; &lt;%= form_for @user do |f| %&gt; &lt;% if @user.errors.any? %&gt; &lt;div class="error_messages"&gt; &lt;h2&gt; Form is invalid&lt;/h2&gt; &lt;ul&gt; &lt;% for message in @user.errors.full_messages %&gt; &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;p&gt; &lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.text_field :email %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :password %&gt;&lt;br /&gt; &lt;%= f.password_field :password %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :password_confirmation %&gt;&lt;br /&gt; &lt;%= f.password_field :password_confirmation %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= radio_button_tag :role_type, "employer", :checked =&gt; false %&gt;&lt;br /&gt; &lt;%= label :role_type, 'Employer' %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= radio_button_tag :role_type, "applicant", :checked =&gt; true %&gt;&lt;br /&gt; &lt;%= label :role_type, 'Applicant' %&gt; &lt;/p&gt; &lt;p class="button"&gt;&lt;%= f.submit %&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <p></p> <p>UsersController which inherits from DeviseController</p> <pre><code>class UsersController &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload