Ruby on Rails: How to Send an Ajax Request to Rails Controller from Login Popup Along with User Given Credentials

Ruby on rails: How to send an AJAX request to rails controller from login popup along with user given credentials

 function login_request(data_to_send)
{
$.ajax({
type: 'post',
url: 'controller/action',
data: { key: "value" key_1: "value_1" },
success: function(data) {
// paste your code here after success
}
});
}

#in your config/route.rb

match 'controller/action', :to => 'controller#action' ,:via => [:get,:post] # i'm using get as well as post also.

#hope that help you. if you face any problem please let me know

Devise ajax modal login not working with Rails 5

Well, after reading more about the issue on the web I developed my solution based on this article (not in English, but I believe it's fair to mention the source).

I guess my problem was mostly with firing ajax events.

sessions/new view

<%= form_for(User.new, url: session_path(:user), :html => {:id => "login-box", :class => "contact-form", :'data-type' => 'json'}, :remote => true ) do |f| %>

Devise config (back to defaults)

config.http_authenticatable_on_xhr = true

Sessions controller

class Users::SessionsController < Devise::SessionsController

respond_to :html, :json

end

That's right, no extra code to controller. And this works both for html and ajax requests. It can be useful if you have some with required current_user and you have redirect_to :login_path (for example creating new post)

Finally and most importantly this
jquery
code works perfect for me

$(document).on('ajax:success', '#login-box', function(e) {
return $.magnificPopup.close();
window.location.reload();
});

$(document).on('ajax:error', '#login-box', function(event, xhr, settings, exceptions) {
var error_messages;

error_messages = xhr.responseJSON['error'] ? "<div class='alert alert-danger pull-left'>" + xhr.responseJSON['error'] + "</div>" : xhr.responseJSON['errors'] ? $.map(xhr.responseJSON["errors"], function(v, k) {
return "<div class='alert alert-danger pull-left'>" + k + " " + v + "</div>";
}).join("") : "<div class='alert alert-danger pull-left'>Unknown error</div>";
return $('#login-box').prepend(error_messages);
});

Invalid confirmation token caused by invalid confirmation query parameter?

In app/views/devise/mailer/confirmation_instructions.html.slim try changing this

a href="#{confirmation_url(@resource, :confirmation_token => @resource.confirmation_token).html_safe}" Confirm my account

Into this

= link_to "Confirm my account", confirmation_url(@resource, :confirmation_token => @resource.confirmation_token)

UPD.

Case closed. Nothing's wrong with the link, that's just artifacts of MIME quoted-printable encoding.

(1)   (General 8bit representation) Any octet, except a CR or
LF that is part of a CRLF line break of the canonical
(standard) form of the data being encoded, may be
represented by an "=" followed by a two digit
hexadecimal representation of the octet's value. The
digits of the hexadecimal alphabet, for this purpose,
are "0123456789ABCDEF". Uppercase letters must be
used; lowercase letters are not allowed. Thus, for
example, the decimal value 12 (US-ASCII form feed) can
be represented by "=0C", and the decimal value 61 (US-
ASCII EQUAL SIGN) can be represented by "=3D". This
rule must be followed except when the following rules
allow an alternative encoding.

(5) (Soft Line Breaks) The Quoted-Printable encoding
REQUIRES that encoded lines be no more than 76
characters long. If longer lines are to be encoded
with the Quoted-Printable encoding, "soft" line breaks

Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies (RFC 2045)

So,

<a href=3D"http://swing.vhost/users/confirmation?confirmation_token=3D=
b2cd338fbbd4b19f11c69ba4d8bffe2876ae853c7d86ca058ef0e919e24e15aa">Confirm=
my account</a>

Is just regular link: =3D is equals sign (=), and single = at the end of line is soft line break (i.e. sign that no line break should be inserted here).

Updating modal popup (facebox) with ajax in rails

If you are submitting form through Ajax (:remote => true) then you can send the JavaScript response from the server on validation fails. This response will display the error messages.
If you have any further query then go ahead :)

How to show any custom error messages in modal by ajax request Ruby on Rails

This will work for you. The errors is used only with objects. We can use it as is in instance methods not in class methods.

Following approach is the standard way to achieve model level validations before saving record to database.

Model :

  class SiteAdmin < ApplicationRecord
self.table_name = 'users'
before_save :search

def search
already_exist = SiteAdmin.where('email = ?', self.email).limit(1)
errors.add(:base, "email already exists ") if already_exist
end
end

Controller :

def create
respond_to do |format|
@site_admin = SiteAdmin.new(site_admin_params)
if @site_admin.save
@site_admin.update_column(:confirmed_at, Time.now)
password = Digest::SHA1.hexdigest([Time.now, rand].join)[0..10]
user = User.where(email: @site_admin.email).first_or_initialize
user.attributes = {password: password, password_confirmation: password, role: 'admin'}
UserMailer.send_credentials(@site_admin.email, password).deliver_now if user.save!
format.json {head :no_content}
format.js {flash[:notice] = 'Site Admin has been created successfully.'}
else
format.json {render json: @site_admin.errors.full_messages,
status: :unprocessable_entity}
end
end
end


Related Topics



Leave a reply



Submit