I have what I think is a very simple problem. I'm coming from a PhP background, and used to do this all the time, so I may be looking at this the wrong way.
I am trying to create an ajax handler in RoR. When the user clicks a button, javascript fires off a POST, and gives the user feedback using the "success:" parameter of jQuery's ajax function.
The problem is, RoR is trying to load a view for the ajax handler, when I really just need a few lines in the controller to do the database work, and echo out a status code that will be interpreted by the user's javascript.
This is all just a mailchimp subscribe holding page, so I am only using the 'home' controller.
My Routes;
map.root :controller => 'home'
map.connect '/mcsubscribe', :controller => 'home', :action => 'mcsubscribe'
My Home Controller;
class HomeController < ApplicationController
def index
# no content
end
def mcsubscribe
print params[:email]
end
end
And my testing javascript, just so you understand what's going on;
function mcSubscribe() {
var email = jQuery("#signup_input_email").val();
jQuery.ajax({
type: "POST",
url: "http://domain.com/mcsubscribe",
data: "email=" + email,
cache: false,
success: function(result) {
alert(result);
}
});
}
I thought this would be a common problem, but I've googled around and only managed to find suggestions to redirect, as the user will never visit the /mcsubscribe page, that doesn't seem appropriate.
In Rails 5 you need to use 'plain':
render plain: params[:email]
Have a look at http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
You might need render :nothing => true
EDIT: misread your question, render :text => "yourtext"
should fit your needs
Using
print params[:email]
will just print that value to the application logs, not into the response.
You want this:
render :text => params[:email]
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