开发者

I am a newb to JSON, and am trying to build a multiple page AJAX form with it

I apologize for sounding daft in advance, but I recently discovered JSON in Rails and I'm really interested in creating a form that is broken up through several pages. The data would then be carried from page to page through a JSON object.

I'm stuck on how to properly to do that.

My first page of the form is this :

First Page

Controller:

def create
  # removed some code here for the sake of simplicity
  if @card_signup.save
    respond_to do |wants|
      wants.html {}
      wants.json { render :json => { :html => (render_to_string :partial => 'final_form')}}
    end
  else
    respond_to do |wants|
      wants.html { render :new }
      wants.json { render :json => { :errors => @card_signup.errors } }
    end
  end
end

The Javascript that handles saving the first form to this controller

$(".step_two_submit").click(function(){

  $("#new_card_signup").attr("action", function(index, action) {
    var values = [],
        getKey;

    $("#sortable").sortable('serialize').replace(/(\w+?)\[]=(\d+?)/g, function(all, key, number) {
        values.push(number);
        getKey = key;
    });

    var cardSignupGetParams = 'card_signup=' + getKey + '[' + values.join(', ') + ']';
    return action + '?' + cardSignupGetParams + "&foo[]=bar";
  });

});

Here's the tough part! I want to make it so that I validate if this form was written correctly as usual. No problem, the form is set up to do that. But when they click next, I want them to go to a second form which is a disclaimer they have to agree to before the Object is saved.

So if they back out, or don't approve of the disclaimer, the Object will have never been created.

To accomplish this.. I assume I need to instead of sav开发者_Go百科ing the object, I need to store the object in a JSON object and carry it over via AJAX to this next AJAX loaded disclaimer page.

Does anyone know how I might be able to splice that together?

Again, I apologize if this doesn't make much sense.


What you really want to do is validate the object before you go to the second part of the page. If you definitely want to make a round trip to the server, you can use @card_signup.valid? instead of @card_signup.save in the controller of your "check" action, and then just hide the form elements with javascript and show your disclaimer. This way you are still checking that it would save when it's finally submitted, but not actually saving it into the database.

Another way to do this without javascript would be to have the first page submit to the controller action that just checked for a valid object, and go to a second page which then renders most of the form elements as hidden elements, effectively carrying the filled-in form to the second page without saving it.

Alternately, you can do the validation on the client side as much as possible and not hit the server. See the Client Side Validations railscast for an interesting and easy-ish way to get this done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜