Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: how do I use javascript conditional on controller/action

Is there an accepted way for generating the javascript in the application layout, dependent upon what the controller/action is?

like image 302
Michael Avatar asked Jan 18 '11 07:01

Michael


1 Answers

content_for is the right way to go about here. Based on the current action you could include javascripts required for the particular view. Also however it should be only in the head as loading javascripts in the middle of the page is considered to be obtrusive and hence have a content for in the head of the page. like

<head> <%= yield :dynamic_javascripts %> </head>

and

<% content_for :dynamic_javascripts do %>
  <%= javascript_include_tag "javascript.js" %>
<%end%>
like image 111
Kunday Avatar answered Sep 20 '22 03:09

Kunday