Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Cameron Luken
Cameron Luken
7,850 Points

Form validation using Ajax in Rails Application

I am trying to create a ruby application that requires users to create an account. One of the requirements is that they need to enter a unique username. I would like to have it so that as they type their username in the username field, it is checking to see if it is taken and if it is, it lets them know automatically. I know this needs to be done using ajax but I cannot figure out how to accomplish this! Thanks for the help!

3 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

You want that after they stop typing (or if they push a button) an ajax request is sent to your users controller for a method like check_username.

That method searches all users and returns either yes (its taken) or no (not available) and then the ajax can complete and tell your user if they are good to go or not

So you need the following

  • ajax request in javascript
  • method in your controller
  • route for that method

I suggest start building something and then asking questions when you get stuck. Happy coding!

Cameron Luken
Cameron Luken
7,850 Points

So the goal is to have it after they stop typing an ajax request is sent out to check if it is taken or not. I have the form written, basically it looks like this:

<%= bootstrap_form_for(@user) do |f| %>
<%= f.alert_message "Please fix the errors below.", error_summary: false %>

<%= f.text_field :full_name %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.text_field :username %>

<%= check_box_tag :remember_me %>
<%= label_tag :remember_me, "Keep you signed in?" %>

<%= f.check_box :terms_of_service, label: "I agree to the Terms of Service" %>

<%= f.submit "Join!" %>

<% end %>

Now that I have the form completed and working correctly, I just don't know the next step to take. I understand that I have to write some javascript but I don't know how to make it work with ruby. Also, I am not sure what I have to write for a method in my controller. I am assuming it is within my user controller. Maybe there is a gem to make this process easier? I know that I will want to use ajax in other parts of the web app so using some type of gem may be the right way to go. I am just so confused on this stage of the project! I have tried to Google different tutorials but I have not found anything really useful to me related to this topic. Thanks so much for the guidance!