Topic: How to load partials on a multiple page form in Rails
I have a website where users can register. I have a registration form which consists of three partials (_step1.html.erb thru _step3.html.erb).
user.rb contains an array of the partial names and a method to go to the next step:
def current_step
@current_step || steps.first
end
def steps
%w[step1 step2 step3]
end
def next_step
self.current_step = steps[steps.index(current_step) + 1]
endThe create action is what moves you to the next step:
def create
@user = User.new(params[:user])
@user.current_step = session[:user_step]
@user.next_step
session[:user_step] = @user.current_step
render :new
endThe new.html.erb files renders the following partial when one clicks 'Next' on the form:
<%= form_for(@user, :html => { :class => "form-horizontal" }) do |f| %>
<div id="partial">
<%= render @user.current_step, :f => f %>
</div>
<%= f.submit "Next", class: "btn btn-primary", :name => "next_button" %>My question is how to get each partial to load using AJAX? My idea is to do something like this:
$("#partial").html("<%= escape_javascript(render @user.current_step) %>");But this doesn't work. Any suggestions?