Adding Javascript in Rails - javascript

I'm using Rails 3.0. I want to add a Javascript file show_javascript.js that is in /public/javascripts/ to a view show.html.erb.
The show.html.erb file takes the <head>...</head> part from the template application.html.erb
I'm wondering how I should add it.

Assuming you have a generic stylesheet layout you could add the following to application.html.erb:
<%= javascript_link_tag 'show' if params[:action] == 'show' %>
You could even use content_for params plus yield in the <head> section like so:
layout.html.erb
<head>
<%= yield(:header) if #content_for_header %>
</head>
product/show.html.erb
<% content_for :header do -%>
<%= javascript_link_tag 'show_product' %>
<% end -%>

Related

Upgrading from Foundation 5.0.3 to 5.1.0 Breaks JavaScript

I'm working on a Rails application which was using Foundation 5.0.3. This had a few issues with Rails 4, but I had it working with no issues. I am not using TurboLinks.
I've just updated to Foundation 5.1.0, and all Foundation JavaScript has stopped working. As far as I can tell, nothing else has changed, there are no resources returning 404, and there are no JavaScript errors coming out of the browser console.
I don't know if the problem is Foundation, the asset pipeline, or my project setup and page structure, but I've tried everything I can think of to debug this with no effect.
Here's my basic page structure in case the error is in how I've set it up, but this was working fine for Foundation 5.0.3.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<%= favicon_link_tag "favicon.png" %>
<%= stylesheet_link_tag "application", media: "all" %>
<%= stylesheet_link_tag params[:controller] %>
<%= stylesheet_link_tag "site_area_specific/#{params[:site_area]}" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag params[:controller] %>
<%= csrf_meta_tags %>
<script>
$(function() {
$(document).foundation();
});
</script>
</head>
<body>
<%= render "shared/topnav" %>
<%= yield %>
<%= render "shared/footer" %>
</body>
</html>

How can I require a javascript file for a particular resource in a ruby on rails site? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using Rails 3.1, where do you put your “page specific” javascript code?
I have a controller called course store and I wish to include an external js file from a third party only on that page. How can I do this?
Something like this should work...
<%= javascript_include_tag( "http://www.example.com/xmlhr" ) if params[:controller] == 'course_store' %>
You can do with content_for,
<% content_for :head do %>
<%= javascript_include_tag 'admin' %>
<% end %>
or
<% content_for :head do %>
<script type="text/javascript">
$(function() {
...
...
});
</script>
<% end %>
More : http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for

Rails 3 - sortable list not working together with wysihtml5

I am using sortable list like described here: http://webtempest.com/sortable-list-in-ruby-on-rails-3-almost-unobtrusive-jquery/
application.html.erb is looking like this:
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js", "rails.js" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
<%= yield :javascript %>
</body>
</html>
The google apis are needed for the sortable list, but if I use those no other javascript is working, in my case the wysihtml5-editor. It works if I delete the google-api, but then the lists are not sortable.
I had imported query twice, one time over the asset pipeline with the "jquery-rails" gem, one time via the javascript_include_tag. What I missed to import via the asset pipeline was query-ui. After that I could remove the javascript_include_tag. Stupid me!

Ruby on Rails 3 javascript_include_tag

When I have this as my javascript_include_tag in the head section application.html.erb:
<%= javascript_include_tag :defaults %>
I can successfully use disable_with on submit_tag, but when I change it to:
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"%>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" %>
<%= javascript_include_tag "application" %>
..in order to use a jQuery calendar I cannot use disable_with anymore. It doesn't work anymore.
Any ideas?
You're missing rails.js add that as well to your includes
<%= javascript_include_tag "rails" %>

Does wdCalendar work with Rails 3.0?

Does wdCalendar work with Rails 3.0?
Yes, it can. Essentially the 'jqcalendars_controller.rb' (or whatever you called it) needs to duplicate the functionality of the 'php/datafeed.php' which is included in 'wdCalendar.zip'. 'datafeed.php' is only sortof RESTful. I recommend taking the time to clean it up and make it conform to that standard. Rails 3.1 will require you to remove 'images/', 'stylesheets/' and 'javascripts/ from all of the respective urls to agree with the new asset pipeline standard.
While you are at it, make sure these are in the tag (and not the ugly html style) of your layout.
<%= stylesheet_link_tag 'dailog' %>
<%= stylesheet_link_tag 'calendar' %>
<%= stylesheet_link_tag 'dp' %>
<%= stylesheet_link_tag 'alert' %>
<%= stylesheet_link_tag 'main' %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'Plugin/Common' %>
<%= javascript_include_tag 'Plugin/datepicker_lang_US' %>
<%= javascript_include_tag 'Plugin/jquery.datepicker' %>
<%= javascript_include_tag 'Plugin/jquery.alert' %>
<%= javascript_include_tag 'Plugin/jquery.ifrmdailog' %>
<%= javascript_include_tag 'Plugin/wdCalendar_lang_US' %>
<%= javascript_include_tag 'Plugin/jquery.calendar' %>
Good luck!
Also, much of the javascript is not unobtrusive. It would be great to get that out of the view.

Categories

Resources