Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ananya Jha
7,058 PointsRendering a partial in devise registration
I am trying to render partials in the devise gem's existing registration form but the 'Sign Up' button is not working. When I remove the render partial line, the signup form behaves normally.
There are 2 types of users in this application. The User model has a polymorphic association to 'Students' and 'Professors' (types of users). So after competing the User registration fields, I am trying to get data particular to the user type by rendering a partial into the devise registration.
The devise registration view is as follows:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
</div>
<div>
<%= f.label :middle_name %><br />
<%= f.text_field :middle_name %>
</div>
<div>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div>
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<div>
<%= f.label :password %>
<% if @validatable %>
<i>(<%= @minimum_password_length %> characters minimum)</i>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div id="select_type">
<%= f.label :user_type %><br />
<%= f.select :loginable_type, options_for_select(%w[Professor Student]) %>
</div>
<div id="student_reg">
<%= render :partial => "/students/form" %>
</div>
<div id="prof_reg">
<%= render :partial => "/professors/form" %>
</div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
The partials are as follows:
<%= form_tag new_professor_path :method => 'post' do %>
<%= label_tag :degree, 'Degree' %><br />
<%= text_field_tag :degree, params[:degree] %>
<% end %>
<%= form_tag new_student_path :method => 'post' do %>
<%= label_tag :reg_number, 'Registration Number' %><br />
<%= text_field_tag :reg_number, params[:reg_number] %>
<% end %>
I have tried creating the partials using form_for but that shows another type of error.
2 Answers
Andy Valdez
2,539 PointsI think I understand what you are trying to accomplish. I've had issues with forms within forms. That has never worked for me. Since you are trying to include Model associations in a single form, you need complex form helpers. The specific example I am talking about from that link is this:
<%= form_for @person do |f| %>
Addresses:
<ul>
<%= f.fields_for :addresses do |addresses_form| %>
<li>
<%= addresses_form.label :kind %>
<%= addresses_form.text_field :kind %>
<%= addresses_form.label :street %>
<%= addresses_form.text_field :street %>
...
</li>
<% end %>
</ul>
<% end %>
So your code might need to look something like this (not tested but should lead you on the right track).
<%= f.fields_for :professor do |p_form| %>
<%= label_tag :degree, 'Degree' %><br />
<%= p_form.text_field :degree, params[:degree] %>
<% end %>
Of course, this is a simplified example and you have to read through the docs to really get an understanding of this. That being said, your situation is polymorphic, so the above code may not work in that instance. I haven't messed with polymorphic associations yet. My suggestion would be to test it out first. If it does to work, use form_helpers to generate the form stuff you need and manually access those params in the controller rather than try and get the rails magic to do the work for you.
Geoff Parsons
11,679 PointsLooks like you've got nested <form> tags which won't work out too well.
If these fields are not to be stored on user you could try adding them to a fields_for and setting accepts_nested_attributes on your user class to allow the devise form to save them. Otherwise you may have to override devise's registration controller to extract these params
Ananya Jha
7,058 PointsThanks, I'll have to try this out and see if it works.
Ananya Jha
7,058 PointsAnanya Jha
7,058 PointsI'll try this out and see if it works