{"id":12226,"date":"2016-05-03T12:11:04","date_gmt":"2016-05-03T09:11:04","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=12226"},"modified":"2016-04-28T09:58:46","modified_gmt":"2016-04-28T06:58:46","slug":"unobtrusive-javascript-via-ajax-rails","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/","title":{"rendered":"Unobtrusive JavaScript via AJAX in Rails"},"content":{"rendered":"<p>For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without reloading the page. Let\u2019s take a look at how we can accomplish this with AJAX in Rails.<\/p>\n<p>Now, many of the brief AJAX examples I\u2019ve come across show variations of retrieving collections of data from the server. You would then handle that data in JavaScript to manipulate your page.<\/p>\n<p>The logical conclusion seems to be to learn a front-end JavaScript framework, but I\u2019ve never liked the idea of having to write a separate front end and back end to a website when it would be simplest to have just one code base for one site.\u00a0This really seems like overkill for web design.<\/p>\n<p>But then I came across UJS in Rails, and it solved my need to have just one code base for website design. Unobtrusive JavaScript is generally known as writing your JavaScript outside of your HTML\u2019s page structure so as to not\u00a0pollute\u00a0your HTML structure.<\/p>\n<p>Overall, I was quite surprised at how easy it is in Rails to have a dynamic website.\u00a0The way Rails has incorporated AJAX does more than what my earlier research had suggested AJAX was for and what it could do.\u00a0The beauty of it is that I can run my JavaScript from the server on the client\u2019s browser without any complicated AJAX response processing.\u00a0This makes me happy.<\/p>\n<p>Now along with the benefit of having one code base, we also have jQuery included in Rails. jQuery offers a higher level of JavaScript methods designed to work the same\u00a0across all browsers, and it allows for cleaner and more legible code.\u00a0If you add Twitter\u2019s Bootstrap with this, you can also integrate many common components into your user\u2019s experience that are also cross-browser compliant. If you style your website with jQuery and Bootstrap, you can worry less about what the site will look like in each browser and spend more time thinking about the user\u2019s experience.<\/p>\n<h2>Rails\u2019 AJAX<\/h2>\n<p>In Rails, submitting an AJAX request can be done as easily as adding <strong>remote: true<\/strong> to a link, button, or form. From there you can have any response be some JavaScript code waiting on the server side, and it will execute in the client\u2019s browser.\u00a0Here\u2019s the simplest code example of UJS via AJAX in a link.<\/p>\n<p>Assuming you have a resource generated in Rails, let\u2019s modify the show route to pop up an alert of the current ID set in a parameter. We\u2019ll call the resource <strong>Thing<\/strong>.\u00a0The show resource for <strong>Thing<\/strong> routes from <strong>\/things\/:id<\/strong>, so <strong>\/things\/1<\/strong> would set the parameter <strong>:id<\/strong> to 1.\u00a0Make sure the controller\u2019s <strong>before_action<\/strong> doesn\u2019t <strong>set_thing<\/strong> for the <strong>:show<\/strong> method for this example. Modify the <strong>:show<\/strong> method as follows in <strong>ThingsController<\/strong>.<\/p>\n<pre class=\"brush:php\"># \/app\/controllers\/things_controller.rb\r\nclass ThingsController &lt; ApplicationController\r\n  def show\r\n    render js: \"alert('The number is: #{params[:id]}')\"\r\n  end \r\nend<\/pre>\n<p>In the index page view for <strong>Thing<\/strong>, we\u2019ll create a link that calls the show resource via AJAX.<\/p>\n<pre class=\"brush:php\">&lt;%# \/app\/views\/things\/index.html.erb %&gt;\r\n&lt;%= link_to 'Number Alert', thing_path(42), remote: true %&gt;<\/pre>\n<p>When you visit your index page, you will see a link that says <em>Number Alert<\/em>.\u00a0Click it, and an AJAX request is sent to the show route for <strong>Thing<\/strong>.\u00a0The <strong>:id<\/strong> parameter is set from the router, and the controller renders the JavaScript response and alerts the user that \u201cThe number is: 42.\u201d<\/p>\n<p>And there\u2019s the beauty of it.\u00a0You didn\u2019t have to write anything to handle an AJAX response.\u00a0You simply wrote JavaScript, and it just works.\u00a0But it gets better.<\/p>\n<p>Unless you\u2019re writing some very short JavaScript responses, you won\u2019t want to be putting JavaScript directly in your controller.\u00a0If we rename our default show template for <strong>Thing<\/strong> from <strong>show.html.erb<\/strong> to <strong>show.js.erb<\/strong>, we can drop our JavaScript in there, remove the render command from the controller, and we get the same behavior.<\/p>\n<pre class=\"brush:php\"># \/app\/controllers\/things_controller.rb\r\nclass ThingsController &lt; ApplicationController\r\n  def show\r\n  end \r\nend<\/pre>\n<p>And the view for show with erb substitution would be written as:<\/p>\n<pre class=\"brush:php\">&lt;%# \/app\/views\/things\/show.js.erb %&gt;\r\nalert('Number &lt;%= params[:id] %&gt;')<\/pre>\n<p>Clicking the link will produce the same result:\u00a0\u201cThe number is: 42.\u201d<\/p>\n<h2>Targeted Changes<\/h2>\n<p>To create the dynamic content we want, we need to target the individual areas of our webpage that we\u2019d like to change.\u00a0For this, we\u2019ll start using jQuery with CSS selectors to select the part of the page to change. CSS selectors refer to HTML document parts by either their pure HTML tag name, their id label proceeded\u00a0by a pound sign, or class\u00a0proceeded\u00a0with a dot. We\u2019ll use the index page for the <strong>Thing<\/strong> model as an example.<\/p>\n<pre class=\"brush:php\">&lt;%# \/app\/views\/things\/index.html.erb %&gt;\r\n&lt;%= link_to 'Change Below', thing_path(42), remote: true %&gt;\r\n\r\n&lt;div id=\"target-for-change\"&gt;\r\n  Now you see \"ME\"!\r\n&lt;\/div&gt;<\/pre>\n<p>Next we\u2019ll create a partial HTML page for replacing the content inside the div tags.<\/p>\n<pre class=\"brush:php\">&lt;%# \/app\/views\/things\/_show.html.erb %&gt;\r\nNow you don't!<\/pre>\n<p>In our show template, we\u2019ll target the div id for the index page and render that partial content as HTML inside it.<\/p>\n<pre class=\" brush:php\">&lt;%# \/app\/views\/things\/show.js.erb %&gt;\r\n$(\"#target-for-change\").html(\"&lt;%= j render(partial: 'show') %&gt;\");<\/pre>\n<p>If you click the <em>Change Below<\/em> link, the content changes instantly from <em>Now you see \u201cMe\u201d!<\/em> to <em>Now you don\u2019t!<\/em><\/p>\n<p>There\u2019s a lot happening here, so let me clarify the different parts in <strong>show.js.erb<\/strong>.\u00a0First of all, the dollar sign is a JavaScript reference to jQuery; the parenthesis that follows allows you to select a part of your webpage with a CSS selector.\u00a0In this case, we\u2019re selecting anything that has the id of target-for-change.<\/p>\n<p>Next, we call the jQuery method on the selected part of the page to replace the HTML within the selected content to the string provided.\u00a0The ERB\u00a0part you see with <strong>render(partial: \u2018show\u2019)<\/strong> gets the HTML from <strong>_show.html.erb<\/strong>, and the <strong>j<\/strong> method string-escapes it in order to be acceptable as a proper string value in JavaScript.\u00a0Anytime you render a \u201cpartial,\u201d the file name will always be proceeded with an underscore so there is no confusion between <strong>_show.html.erb<\/strong> and <strong>show.js.erb<\/strong> in template rendering.<\/p>\n<h2>Design<\/h2>\n<p>Now that you have the ability to dynamically update the content of your website with one code base, the possibilities are endless.\u00a0You can defer labor-intensive calculations to be displayed on a webpage after the page has already loaded, such as calculating unread messages or connection requests. If you have the server query the database to count all the records before sending the page, it will slow down page load time and create a bad user experience.<\/p>\n<p>If instead you let the page load quickly and then trigger an AJAX request for a \u201cslow\u201d query, you won\u2019t interfere with the user\u2019s experience. Rather you\u2019ll improve it, and the content will update in no time.<\/p>\n<p>When creating custom routes for your UJS requests, it\u2019s important to make sure you\u2019re using the right method of request.\u00a0I generally like to use the PATCH method for UJS requests. Along with adding <strong>remote: true<\/strong> to my links, buttons, or forms, I also add <strong>method: :patch<\/strong>.<\/p>\n<pre class=\"brush:php\"># \/config\/routes.rb\r\nRails.application.routes.draw do\r\n  resources :things\r\n\r\n  scope :ujs, defaults: { format: :ujs } do\r\n    patch 'thing_totals' =&gt; 'things#totals'\r\n  end \r\nend<\/pre>\n<p>I like to both scope UJS to <strong>\/ujs<\/strong> routes and declare in the routes that the format is <strong>:ujs<\/strong>.\u00a0This isn\u2019t strictly necessary, but I believe it\u2019s important to be clear about the intent of the routes in use.\u00a0The relevant part of the code here is simply the patch method, which routes to our <strong>ThingsController#totals<\/strong> method.<\/p>\n<pre class=\"brush:php\"># \/app\/views\/things_controller.rb\r\nclass ThingsController &lt; ApplicationController\r\n  def totals\r\n    value = 42 # Some expensive database query\r\n    render js: \"$('#dashboard-totals').html('#{value}')\"\r\n  end \r\nend<\/pre>\n<p>Next I\u2019ll demonstrate an index page with a dashboard that delays retrieving the totals from the server until after the page is loaded.<\/p>\n<pre class=\"brush:php\">&lt;%# \/app\/views\/things\/index.html.erb %&gt;\r\n\r\nDashboard totals: &lt;span id=\"dashboard-totals\"&gt;&lt;%=\r\n  link_to \".\",\r\n  url_for({controller: \"things\", action: \"totals\"}),\r\n  method: :patch, remote: true,\r\n  class: \"dashboard-totals\"\r\n%&gt;&lt;\/span&gt;\r\n&lt;%= javascript_tag '$(\".dashboard-totals\").click();' %&gt;<\/pre>\n<p>When the page first loads, the dashboard totals show a period as a link: <strong>Dashboard totals: .<\/strong><\/p>\n<p>But once the page has fully loaded, the JavaScript from the <strong>javascript_tag<\/strong> gets executed.\u00a0Using jQuery, it selects everything with the class of <strong>dashboard-totals<\/strong> and then hits the link.\u00a0This submits the AJAX request for the link to the right controller action via the patch method, and the server returns the JavaScript response. This replaces the content inside the span tag with the appropriate value since it\u2019s identified as <strong>dashboard-totals<\/strong>. So the page seems to immediately have the value 42 for the dashboard total.\u00a0This is a great tool for deferred content loading!<\/p>\n<h2>Bootstrap<\/h2>\n<p>Bootstrap has many kinds of components\u00a0you can inject into your page, some of which need to be called via a JavaScript method call to be seen, like the modal.<\/p>\n<p>A modal in Bootstrap is like a windowed dialog that grays out your webpage as it comes into focus.\u00a0When you insert the HTML code into your page for a modal via an AJAX request, it\u2019s not visible until you activate it.\u00a0Since the code won\u2019t interfere with your page\u2019s layout, you may add an HTML tag to your main Rails layout page near the end of the body\u2019s content \u2014 something simple, like <code>&lt;div id=\"target-modal\"&gt;&lt;\/div&gt;<\/code>. This won\u2019t show up on your page, and once you fill the content with the modal, there is still no visible change to your page.<\/p>\n<p>The details for <a href=\"http:\/\/getbootstrap.com\/\">how to implement all of Bootstrap\u2019s various features<\/a> are available here.\u00a0But as for the UJS view, you can simply enter another line to call the modal, and it promptly displays itself.<\/p>\n<pre class=\"brush:php\">$(\"#target-modal\").html(\"&lt;%= j render(partial: 'suggestions\/new_suggestion') %&gt;\");\r\n$('#newSuggestionModal').modal();<\/pre>\n<p>Another Bootstrap component I like using in order to dynamically load content is collapsible items. I defer some of the page\u2019s content with <em>expand to see more<\/em> links, which will grab the information from the server, fill in the content, and then slide open the content in place as if it were hidden under a rug. This way you don\u2019t have to query the database for so much information all at once and make a big slow view. For example, you can show previews for each contact with details available via a click.<\/p>\n<h2>Summary<\/h2>\n<p>Anything you thought you would need a JavaScript front end framework for in Rails, you actually <em>don\u2019t<\/em>. Rails really stands up as an all-in-one solution. You can also add the ability to dynamically write JavaScript with Opal or CoffeeScript, and then you\u2019re in quite a nice place to be.<\/p>\n<p>Having one code base just makes sense when it comes to web design. And with Rails, you can have as dynamic a site as you want with a simple JavaScript template and the unseen AJAX communications all handled for you.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"https:\/\/blog.codeship.com\/unobtrusive-javascript-via-ajax-rails\/\">Unobtrusive JavaScript via AJAX in Rails<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\">WCG partner<\/a>\u00a0Daniel P. Clark at the <a href=\"http:\/\/blog.codeship.com\/\">Codeship Blog<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without reloading the page. Let\u2019s take a look at how we can accomplish this with AJAX in Rails. Now, many of the brief AJAX examples I\u2019ve come across show variations &hellip;<\/p>\n","protected":false},"author":146,"featured_media":4127,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[34,95],"class_list":["post-12226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-ajax","tag-rails"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unobtrusive JavaScript via AJAX in Rails - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unobtrusive JavaScript via AJAX in Rails - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-03T09:11:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Daniel P. Clark\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Daniel P. Clark\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\"},\"author\":{\"name\":\"Daniel P. Clark\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e\"},\"headline\":\"Unobtrusive JavaScript via AJAX in Rails\",\"datePublished\":\"2016-05-03T09:11:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\"},\"wordCount\":1706,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg\",\"keywords\":[\"Ajax\",\"Rails\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\",\"name\":\"Unobtrusive JavaScript via AJAX in Rails - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg\",\"datePublished\":\"2016-05-03T09:11:04+00:00\",\"description\":\"For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Unobtrusive JavaScript via AJAX in Rails\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e\",\"name\":\"Daniel P. Clark\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g\",\"caption\":\"Daniel P. Clark\"},\"description\":\"Daniel P. Clark is a freelance developer, as well as a Ruby and Rust enthusiast. He writes about Ruby on his personal site.\",\"url\":\"https:\/\/www.webcodegeeks.com\/author\/daniel-clark\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unobtrusive JavaScript via AJAX in Rails - Web Code Geeks - 2026","description":"For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/","og_locale":"en_US","og_type":"article","og_title":"Unobtrusive JavaScript via AJAX in Rails - Web Code Geeks - 2026","og_description":"For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-05-03T09:11:04+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg","type":"image\/jpeg"}],"author":"Daniel P. Clark","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Daniel P. Clark","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/"},"author":{"name":"Daniel P. Clark","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e"},"headline":"Unobtrusive JavaScript via AJAX in Rails","datePublished":"2016-05-03T09:11:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/"},"wordCount":1706,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg","keywords":["Ajax","Rails"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/","name":"Unobtrusive JavaScript via AJAX in Rails - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg","datePublished":"2016-05-03T09:11:04+00:00","description":"For now, the main way to add dynamic content to a webpage is with JavaScript.\u00a0Ideally, we want to update a site\u2019s contents from the server without","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/04\/rubyonrails-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/unobtrusive-javascript-via-ajax-rails\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Unobtrusive JavaScript via AJAX in Rails"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e","name":"Daniel P. Clark","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g","caption":"Daniel P. Clark"},"description":"Daniel P. Clark is a freelance developer, as well as a Ruby and Rust enthusiast. He writes about Ruby on his personal site.","url":"https:\/\/www.webcodegeeks.com\/author\/daniel-clark\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/146"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=12226"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12226\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/4127"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=12226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=12226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=12226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}