Suppose you have a controller HomeController
. It should look like below.
home_controller.rb
class HomeController < ApplicationController # GET /show_message def show_message format.js { render js: "alert('simple output');" } end end
and in your routs.rb
file add this line
get '/show_message', 'home#show_message', as: :show_message
and in your view add this line
<%= link_to "Print", show_message_path, remote: true %>
After this when you click on Print
link, and AJAX request will be sent to HomeController'sshow_message
action and it will display a javascript alert.
For better understanding of AJAX requests in Rails, follow this tutorial - http://www.tutorialspoint.com/ruby-on-rails/rails-and-ajax.htm :)