Page loading Ajax + Jquery + Ruby on Rails

Asked

Viewed 367 times

0

Oops, good afternoon guys I’m a little new in Rails and to with a little difficulty. I have a login form (Sessions/new) and a registration form (users/new), in each of these formularies, I have a link in which when clicked redirects the user to the other form. I would like to do this via Ajax + Jquery, to get a smoother transition. However I never used ajax in Rails. If anyone can give me a light on what to do thank you, a tutorial, or a tip on how to proceed. Thanks from now on.

  • It works the same as in any other language, it’s no secret. You make the request via Ajax informs you where you want the result of this request to be displayed

  • So, I did here, but it seems that only one link works. I removed the require turbolinks to test and only one works without refresh. I believe it’s some silly mistake, my friend. link: http://pastebin.com/VUqxNype

  • Do yourself a big favor and put the code you think you need in your question, so it’s easier.

1 answer

0


In case you are using two actions from two different controllers, so you will be redirected from one to the other via a redirect_to after the creation of Session.

From what I understand you do not want a redirect, but rather that a form appears on the same screen after creating the other, 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 post done in ajax, have 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 your Forms view 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" }
});
  • Opa, thanks for the reply, gave to understand well as you explained. I ended up using the Gem nprogress, it wasn’t exactly what I needed but it’s breaking a branch. I’m going to do some tests the way you said, to see how it looks. Thank you for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.