Soo, I have this validation I am trying to setup using an AJAX and Coffee Script get request. This request calls a method on the RoR controller and is suppose to send back data. However, it is not working. This is my first go at Coffee Script so please be gentle, I'm just a baby.
My error -> ActionController::RoutingError (No route matches [GET] "/home/get_email/[email protected]"):
Do I need to create a route for my AJAX GET request? Code below.
Assume the .change function does it's job and enters my AJAX cal l as expected.
###Coffee Script AJAX###
jQuery ->
$('#txt_email').blur ->
@data
@txtemail = $('#txt_email').val()
$.ajax 'home/get_email/' + @txtemail,
type: "GET",
dataType: "JSON",
asnyc: false,
success: (data) ->
console.log(data)
###Home Controller Method###
def get_email(email)
@db_email = User.where(["email = ?", email]).first
respond_to do |format|
if @db_email != nil
format.js {}
format.json { render json: @db_email }
else
format.html { render action: 'index' }
format.json { render json @db_email.email, status: :unprocessable_entity }
end
end
end
Making some progress. Currently here is what is occurring. Seems like the id param is not being populated, but the "email" text value is coming through. I also updated the Coffee JS to pass the txt_email value properly.
Parameters: {"email"=>"[email protected]", "id"=>"get_email"}
Animal Load (1.0ms) SELECT "animals".* FROM "animals" WHERE "animals"."id" = ? LIMIT 1 [["id", "get_email"]]
###Routes###
home_index GET /home/index(.:format) home#index
home_show GET /home/show(.:format) home#show
profile_new GET /profile/new(.:format) profile#new
root GET / home#index
GET /home(.:format) home#index
POST /home(.:format) home#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/:id/edit(.:format) home#edit
home GET /home/:id(.:format) home#show
PATCH /home/:id(.:format) home#update
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
profile_index GET /profile(.:format) profile#index
POST /profile(.:format) profile#create
new_profile GET /profile/new(.:format) profile#new
edit_profile GET /profile/:id/edit(.:format) profile#edit
profile GET /profile/:id(.:format) profile#show
PATCH /profile/:id(.:format) profile#update
PUT /profile/:id(.:format) profile#update
DELETE /profile/:id(.:format) profile#destroy
Final code
$(txtemail).blur ->
if theEmailIsRegistered($(this).val()) is false
$('#lbl_error_email').html('The entered e-mail is already in use.')
else
$('#lbl_error_email').html('')
theEmailIsRegistered = (email) ->
$.ajax 'home/get_email/',
type: 'GET',
dataType: 'JSON',
data: {email: email }
async: false,
success: (data) ->
if data is true
return true
else
return false
def get_email()
@db_email = User.where(email: params[:email]).first
respond_to do |format|
if @db_email.present? && @db_email.email == params[:email]
format.js {}
format.json { render json: true }
else
format.js
format.json { render json: false }
end
end
end
Custom Route
get 'home/get_email' => 'home#get_email'
I guess the problem is because of controller-action. it is not supposed to be in this format def some_action(some_parameter) ; end. To fix this , you have to do something like below
do this
$.ajax 'home/get_email' ,
type: "GET",
dataType: "JSON",
data:
email: @txtemail
asnyc: false,
success: (data) ->
console.log(data)
And in homes_controller do this
def get_email
@db_email = User.where(email: params[:email]).first
respond_to do |format|
if @db_email != nil
format.js {}
format.json { render json: @db_email }
else
format.html { render action: 'index' }
format.json { render json @db_email.email, status: :unprocessable_entity }
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With