How to include code in only one controller action?-Collection of common programming errors
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’s been removed?
Maybe it would be best to remove the conditional from application.html.erb
altogether and just put it at the top of posts/new.html.erb
?
-
I would avoid delegating responsibility for this to your application layout. If you don’t need jQuery UI on more than a single view, you are best off letting the view handle that. The following let’s you do just that while still keeping your output HTML clean and sensible (ie. not putting JS all over the place willy nilly).
In your layout (
application.html.erb
):Foo Bar
In your view that requires jQuery UI (
posts/new.html.erb
):Note: despite convention, putting unnecessary javascript in the degrades performance.
-
I believe it’s:
controller.action_name
Originally posted 2013-11-09 21:40:11.