{"id":698,"date":"2022-08-30T15:05:41","date_gmt":"2022-08-30T15:05:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-include-code-in-only-one-controller-action-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:41","modified_gmt":"2022-08-30T15:05:41","slug":"how-to-include-code-in-only-one-controller-action-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-include-code-in-only-one-controller-action-collection-of-common-programming-errors\/","title":{"rendered":"How to include code in only one controller action?-Collection of common programming errors"},"content":{"rendered":"<p>I have only a single page that requires jquery ui in my entire application. How can conditionally include the javascript files in that single page?<\/p>\n<p>I believe in Rails 2 I could use: (in application.html.erb)<\/p>\n<pre><code>\n   \n  \n\n<\/code><\/pre>\n<p>But <code>controller.controller_action<\/code> throws an undefined method error. And after looking at the API, it looks like it&#8217;s been removed?<\/p>\n<p>Maybe it would be best to remove the conditional from <code>application.html.erb<\/code> altogether and just put it at the top of <code>posts\/new.html.erb<\/code> ?<\/p>\n<ol>\n<li>\n<p>I would avoid delegating responsibility for this to your application layout. If you don&#8217;t need jQuery UI on more than a single view, you are best off letting the view handle that. The following let&#8217;s you do just that while still keeping your output HTML clean and sensible (ie. not putting JS all over the place willy nilly).<\/p>\n<p>In your layout (<code>application.html.erb<\/code>):<\/p>\n<pre><code>\n  Foo Bar\n  \n\n<\/code><\/pre>\n<p>In your view that requires jQuery UI (<code>posts\/new.html.erb<\/code>):<\/p>\n<pre><code>\n   \n  \n\n<\/code><\/pre>\n<p>Note: despite convention, putting unnecessary javascript in the degrades performance.<\/p>\n<\/li>\n<li>\n<p>I believe it&#8217;s:<\/p>\n<pre><code>controller.action_name\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:40:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have only a single page that requires jquery ui in my entire application. How can conditionally include the javascript files in that single page? I believe in Rails 2 I could use: (in application.html.erb) But controller.controller_action throws an undefined method error. And after looking at the API, it looks like it&#8217;s been removed? Maybe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-698","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=698"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/698\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}