Topic: Rendering partial via AJAX call in Rails

I have a multi-part form composed of three partials.  The names of my partials are stored in @user.current_step.  My form looks like this:

<%= form_for(@user, :html => { :class => "form-horizontal" }, remote: true) do |f| %>
  <%= render 'shared/error_messages' %>

  <div id="partial">
    <%= render @user.current_step, :f => f %>
  </div>

  <div class="control-group">
    <div class="controls">
      <%= f.submit "Previous", class: "btn btn-primary", :name => "prev_button" unless @user.first_step?  %>
      <%= f.submit "Next", class: "btn btn-primary", :name => "next_button" %>
    </div>
  </div>
<% end %>

Now to move between the partials I update @user.current_step within my controller when I click on 'Next' or 'Previous'.  To load the partials I try this:

$("#partial").html("<%= escape_javascript(render @user.current_step, f: f) %>");

But this isn't working.  Also my 'Previous' button doesn't show up.  It looks like the code unless @user.first_step? isn't working either. Any ideas?

Re: Rendering partial via AJAX call in Rails

Hi,
Not sure if this is very relevant, but do you know this gem : https://github.com/schneems/wicked ? I did not test it but it seems to be helpful in the creation of a 'step-by-step' form, so you may find some helpful pieces of code to put you on the right way...

Hope this helps a bit...
Cheers

Re: Rendering partial via AJAX call in Rails

<%= render :partial => @user.first_step %>

Might have to add .to_s to the end. I don't know. Then include your form inside the partial. I have no idea what you were trying to do.

Edit:

Your controller renders actions, not your view. Post up your controller code. Your controller should handle the logic and pass it to the js view. When done, it can then redirect! Assuming I understand correctly.

Last edited by tuscanidream (2013-01-18 22:55:29)

Learning rails, one day at a time.

http://keilmiller.com

Re: Rendering partial via AJAX call in Rails

Here's my controller:

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def new
    @user = User.new
    session[:user_step] = nil
  end

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      format.js do
        @user.current_step = session[:user_step]
        if params[:prev_button]
          @user.previous_step
        elsif @user.last_step?
          @user.save
        else
          @user.next_step
        end
        session[:user_step] = @user.current_step
      end
    end
  end
end

And here's my model:

class User < ActiveRecord::Base

  def current_step
    @current_step || steps.first
  end

  def steps
    %w[step1 step2 step3 step4]
  end

  def next_step
    self.current_step = steps[steps.index(current_step) + 1]
  end

  def previous_step
    self.current_step = steps[steps.index(current_step) - 1]
  end

  def first_step?
    current_step == steps.first
  end

  def last_step?
    current_step == steps.last
  end
end

Re: Rendering partial via AJAX call in Rails

I assume you are following this railscasts tutorial: http://railscasts.com/episodes/217-multistep-forms

Dude, your not rendering partials. Re-read my previous post. Change the "render blah" bits to "render :partial => blah" and let me know if it resolves your issues.

Learning rails, one day at a time.

http://keilmiller.com

Re: Rendering partial via AJAX call in Rails

I am rendering partials. The :partial symbol is unnecessary.