IT Staff

Blog về chuyên ngành IT

[RoR] Rails 3.1 issue with javascript_include_tag in application.html.erb

leave a comment »

Rails 3.1 issue with javascript_include_tag in application.html.erb

I’ve created a clean and simple Rails 3.1 application called demo1. It contains two controllers – hello and goodbye. I get the following error when I try to load http://localhost:3000/say/hello:

ActionView::Template::Error (   (in /home/me/dev/ruby/demo1/app/assets/javascripts/     3: <head>     4:   <title>Demo1</title>     5:   <%= stylesheet_link_tag    "application" %>     6:   <%= javascript_include_tag "application" %>     7:   <%= csrf_meta_tags %>     8: </head>     9: <body>   app/views/layouts/application.html.erb:6:in     `_app_views_layouts_application_html_erb___558576499_89622220' 

The problematic line of application.html.erb is:

<%= javascript_include_tag "application" %> 

When I created the application using Rails 3.0 this particular line was:

<%= javascript_include_tag :defaults %> 

And this worked fine. In fact, when I change the application.html.erb to use :defaults everything works, but I want to use the new features of Rails 3.1.

I can’t seem to turn up anything on Google around this, I guess because Rails 3.1 has just been released.

By the way, I’m following the first chapter in the Agile Web Development with Rails (4th edition) Updated for Rails 3.1 book.

Some environmental info that may help in answering this question:

$ cat /etc/issue Ubuntu 10.04.2 LTS \n \l $ ruby -v ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux] $ rails -v Rails 3.1.0 
Contents of the file:
# Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: 

Answer: 1

Ok, I’ve figured out what the issue was, and so I’ll answer my own question.

The problem was the app/assets/javascripts/application.js file contained commented out code. However, one of the commented out lines was as follows:

//= require_tree . 

When I delete this line everything works fine.

Hope this helps. If someone can provide some insight as to why the underscore was causing the issue that would be great.

The line as you put in your answer is actually correct. That will include all javascript files under folders assets/javascripts. See:…. A rails 3.1 app i created earlier today has that and it works fine. I also have two empty coffeescript files. – agmcleod Sep 2 ’11 at 18:13
I am starting off with ROR and faced the same issue, and the same solution works! However I still dont get from the dicussion if deleting the line is something preferable and how is it solving the problem? – Myth17 Sep 18 ’11 at 15:46
Wow, that actually worked! Bung-as bro! – Adam Harte Dec 13 ’11 at 0:30

Answer: 2

In Rails 3.1 there is no longer a “defaults” as such, but rather what is specified in your application.js file are the “defaults”. You would include this file using this line:

 javascript_include_tag "application" 

The jquery and jquery_ujs files come with the jquery-rails gem which is in the default Rails 3.1 Gemfile.

The //= require line in that file tells Sprockets that you want to require a file which in this case would be jquery.js from within jquery-rails, where the //= require_tree . will require all other JavaScript files in the same directory as application.js and concatenate them all into one file.

You can read more about the asset pipeline here.


Written by Xavier

Tháng Sáu 9, 2012 lúc 10:17 sáng

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập: Logo

Bạn đang bình luận bằng tài khoản Đăng xuất /  Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất /  Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất /  Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất /  Thay đổi )


Connecting to %s

%d bloggers like this: