Page loading Ajax + Jquery + Ruby on Rails

0

Oops, good afternoon guys, I'm a little new to rails and to with a little difficulty. I have a login form (sessions / new) and a registration form (users / new), in each of these forms, I have a link in which when clicked redirects the user to the other form. I would like to do this via Ajax + Jquery, for a smoother transition. But I have never used ajax in rails. If someone can give me a light on what to do thank you, a tutorial, or a tip on how to proceed. Thank you in advance.

    
asked by anonymous 04.08.2015 / 20:10

1 answer

0

In case you are using two actions of two different controllers, therefore you will be redirected from one to the other through a redirect_to after the session is created.

So I understand you do not want a redirection, but rather that one form appears on the same screen after the other is created, so leave the two forms in the same action, after sending the data of one, hide it and show the other.

In an action you can have instances for the two forms, while the ajax post has different actions:

RegistrationsController
    def new
      @session = Session.new
      @user = User.new
    end
end

Class SessionsController
    def create
      #create session here
    end
end

Class UserController
   def create
     #create user here
   user
end

In the view of your forms you will have the ajax requests:

#views/registrations/new.html.erb

$.ajax({
  type: "POST",
  url: "/sessions/create",
  data: { "email": "[email protected]", "password": "password" }
});

$.ajax({
  type: "post",
  url: "/user/create",
  data: { "name": "Adolph", "las_name": "Schindler" }
});
    
07.08.2015 / 16:19