Samuel D.
Samuel D.

Reputation: 189

How to store input values ​in simple for form? Rails

I have this simple for form:

<%= simple_form_for @user do |f| %>
  <%= f.input :username, label_html: { class: 'my_class' }, hint_html: { class: 'hint_class' } %>
  <%= f.input :age %>
  <%= f.button :submit %>
<% end %>

I would like to take the values ​​that the user enters to use as conditionals

Take a value of :age input or :username input. Take these two values ​​given by the user without the user giving the submit button

I don't know if there is any way that rails allows to do that, or if not use a simple jquery.

If you could help me I would be very happy. Thank you very much

Upvotes: 1

Views: 44

Answers (1)

LihnNguyen
LihnNguyen

Reputation: 865

You can use simple Jquery

<%= simple_form_for @user do |f| %>
  <%= f.input :username, class: 'username' %>
  <small class="error_username"></small>
  <%= f.input :age, class: 'age' %>
  <%= f.button :submit %>
<% end %>
--------------------------------------------------
form.js
Jquery + Ajax
$(document).ready(function() {
  var timeout = null;
  $('.username').keyup(function() {
     clearTimeout(timeout);
     timeout = setTimeout(function () {       
        checkUserName($(this).val());
     }, 1000)
  });

  checkUserName(username) {
     $.ajax({
        url: "check_user_name",
        type: "post",
        data: {username: username},
        success: function(res) {
            // res = {message: 'Username is existed'}
            $(".error_username").html(res['message'])
        },
        error: function(err) {
           console.log(err)
        }
     })
  }
})




Upvotes: 0

Related Questions