Your problem can be solved in different ways.
Dynamically add assets
Add the following method to our application helper:
module ApplicationHelper def include_related_asset(asset) # v-----{Change this} if !YourApp::Application.assets.find_asset(asset).nil? case asset.split('.')[-1] when 'js' javascript_include_tag asset when 'css' stylesheet_link_tag asset end end end end
Call the helper method in layout -file:
<%= include_related_asset(params[:controller].to_param + '_' + params[:action].to_param . 'js') %>
Create specific assets for the actions of your controller. E. g. controller_action.js
Use yield
See the Rails guide for more information.
To pass controller data to your javascript, you can do:
<%= javascript_tag do %> window.error_message = '<%= j error_message %>'; <% end %>
See RailCast Episode # 324 for more information.
Robin source share