<%= javascript_include_tag 'javascripts/admin/manage_customer.js' %>
=> <script src="/assets/javascripts/admin/manage_customer.js" type="text/javascript"></script>
How to generate simple JS link to /javascripts/admin/manage_customer.jsin Rails 3.1?
Just put a slash before that.
<%= javascript_include_tag '/javascripts/admin/manage_customer.js' %>
Related
Am testing the content_for in my rails 3.2 app and following the rails guides but they are specific to the actual files and I cannot seem to get the yield to work:
application.html.erb file:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<%= yield :navigation %> #shouldn't this load the content_for block named :navigation specified in the _main_nav.html.erb partial?
<%= yield %> #this load the index page content
</body>
</html>
I created a layout file _main_nav.html.erb (i know I can render with <%= render 'layouts/header' %> but I am trying to use the content_for instead) The _main_nav.html.erb is:
<% content_for :navigation do %>
<ul>
<li>Home</li>
</ul>
<% end %>
They way I read the RailsGuide http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method
this should work. But it does not. I do not get an error. Seems simple but I am stumped.
When I go to my index.html.erb file I would expect to see this result:
Home
I believe what you want to have is have a view that will contain your content_for block. So an example would be if you have the following:
index.html.erb
<% content_for :head do %>
<%= stylesheet_link_tag 'users' %>
#Above this will load the users stylesheet
<% end %>
<h2>Example</h2>
<ul>
<% #users.each do |users| %>
<li><%= user.name %></li>
<% end %>
</ul>
Then to output what inside the users stylesheet we can yield and pass in the symbol of the name of the content_for.
Application.html.erb
<!-DOCTYPE html>
<html>
<head>
<%= yield :head%>
<title>This is my title</title
</head>
<body>
<p>This is a test</p>
<%= yield %>
</html>
So to review whats happening here is that, in my example I am saying I have a users stylesheet that I would like to load into the <head></head> of my application.html.erb. To do this I set the content_for which is a Rails helper and give it the identifier sysmbol which is head which is then called in the application.html.erb where I do yeild :head. So what I am getting my application to do is when the my index.html.erb for that page is being rendered the application.html.erb will load my users stylesheet. Hope this clears things up for you.
Update explanation
To add to this another thing the purpose of combination of using content_for with yield is to allow you to inject data into the application layout from ANY view. So as another example. You could have the following:
<% content_for :title do %> My Title<% end %>
Here when the controller renders the view template and combines it with the application layout, the text My title will be replaced. The yield(:head) makes it easy to add more elements to the specific page if needed. Take a look at the following example:
app/views/layouts/application.html.erb
<% if content_for?(:navbar) %>
<%= yield(:navbar) %>
<% else %>
<%# default navbar %>
<section class="navbar"></section>
<% end %>
app/views/blah/index.html.erb
<% content_for(:navbar) do %>
<section class="navbar"></section>
<% end %>
And a further note not sure how your developing your application or what design framework your using but you could also take a look at Rails-Bootstrap-Navbar. May also be an alternative.
OK, I think I have a solution for this. Your code:
<% content_for :navigation do %>
<ul>
<li>Home</li>
</ul>
<% end %>
should be at the top of the file that is loading. Your _header.html.erb is a partial. If you move this code into views/tasks/new.html.erb then it works as expected.
However, for it to work as you want, then you need to adjust your application.html.erb file:
<p>this is where we should see the "Home" link appear that is defined in _header.html.erb:</p>
<section class="header">
<% render 'layouts/header' %>
<%= yield :navigation %>
</section>
Notice, that I have called the render erb tag without the = sign. This means I don't see the contents of the header partial, but it does load. If you include the = sign then it still works but also renders any other content you may have in the partial.
Note: The render tag has to be above/before the yield tag.
I have a model - Products, ProductsController and a layout product. I declared the layout inside the controller. added the css/js/images to the app assets folder. I did everything the rails guide told me to do when I want to have a custom layout. But It still doesn't show the layout i declared but shows me the default page without any formatting and settings.
my files are as follows
Products Controller
class ProductsController < ApplicationController
layout "product"
# GET /products
# GET /products.json
def index
#products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
end
and my layout file is
<!DOCTYPE html>
<html>
<head>
<title>Cool Amazon Products</title>
<%= stylesheet_link_tag "application" %>
<%= stylesheet_link_tag "bootstrap" %>
<%= stylesheet_link_tag "bootstrap-responsive" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Cool Products from Amazon</a>
</div>
</div>
</div>
<%= yield %>
<%= javascript_include_tag "bootstrap" %>
</body>
</html>
the index.html.erb is the one that is generated by default!.
I am using twitter bootstrap for the UI. Please let me know if i'm missing something inorder to display the correct layout.
Ok, weird, that should work.
So, things to check:
Are you in development environment, or have you restarted your Rails app?
Are you using page or action caching, and you're not clearing them?
Could it be a caching issue on the web server?
That's about all that could be causing this.
What will be the erb code for below haml code?
content_for :head do
= stylesheet_link_tag "plupload.queue"
= javascript_include_tag "plupload/plupload.full.min", "plupload/jquery.plupload.queue.min"
= javascript_tag do
= render "plupload.js"
photos_container
= render :partial => "photo", :collection => #photos
uploader
%p You browser doesn't have HTML5, Flash or Silverlight support.
I advise to read HAML and ERB documentation as it basics for your development process. There is a lot of information around this, so try to use search before you ask.
http://guides.rubyonrails.org/layouts_and_rendering.html
http://haml.info/
Your ERB code:
<% content_for :head do %>
<%= stylesheet_link_tag "plupload.queue" %>
<%= javascript_include_tag "plupload/plupload.full.min", "plupload/jquery.plupload.queue.min" %>
<%= javascript_tag do %>
<%= render "plupload.js"%>
<% end %>
<% end %>
<div id="photos_container">
<%= render :partial => "photo", :collection => #photos %>
</div>
<div id="uploader">
<p>You browser doesn't have HTML5, Flash or Silverlight support.</p>
</div>
In Rails 3 you can optimize your code, if you use app/views/photos/_photo.html.erb partial. Rails determines the name of the partial to use by looking at the model name in the collection.
Like this:
<div id="photos_container">
<%= render #photos %>
</div>
Read about rendering in Rails 3:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
The documentation says that a provider key is not needed when using Google Maps. However, I want to use my Google API Key so that I can track my usage.
From the _scripts.html.erb file in the gem, it appears that there is no built-in support for using the provider_key with Google Maps. Is that right?
<% if enable_js == true && scripts.try(:to_sym) != :none %>
<% case map_options.try(:[], :provider) %>
<% when "openlayers" %>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<% when "mapquest" %>
<script src="http://mapquestapi.com/sdk/js/v6.0.0/mqa.toolkit.js?key=<%= map_options.try(:[], :provider_key) %>"></script>
<% when "bing" %>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<% else %>
<script type="text/javascript" src="//maps.google.com/maps/api/js?v=3.5&sensor=false&libraries=geometry<%= gmaps4rails_js_libraries(map_options.try(:[], :libraries)) %>"></script>
<script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js"></script>
<script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.5/src/markerclusterer_packed.js"></script>
<script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js"></script>
<% end %>
<% if Rails::VERSION::MAJOR >= 3 && Rails::VERSION::MINOR < 1 %>
<% unless scripts.try(:to_sym) == :api %>
<%= javascript_include_tag 'gmaps4rails/gmaps4rails.base.js' %>
<% end %>
<% case map_options.try(:[], :provider) %>
<% when "openlayers" %>
<%= javascript_include_tag 'gmaps4rails/gmaps4rails.openlayers.js' %>
<% when "mapquest" %>
<%= javascript_include_tag 'gmaps4rails/gmaps4rails.mapquest.js' %>
<% when "bing" %>
<%= javascript_include_tag 'gmaps4rails/gmaps4rails.bing.js' %>
<% else %>
<%= javascript_include_tag 'gmaps4rails/gmaps4rails.googlemaps.js' %>
<% end %>
<% end %>
<% end %>
If yes, are there any plans to add support for a Google Maps provider key? Or am I better off just hacking it in myself? :-P
<script type="text/javascript" src="https://maps.google.com/maps/api/js?v=3.5&sensor=false&libraries=geometry<%= gmaps4rails_js_libraries(map_options.try(:[], :libraries)) %>"></script>
<script type="text/javascript" src="https://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js"></script>
<script type="text/javascript" src="https://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.5/src/markerclusterer_packed.js"></script>
<script type="text/javascript" src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js"></script>
src modification is needed.
I'm following one of the best tutorials/books I have ever come across, the Ruby On Rails Tutorial
but I am trying to include the "blueprint" stylesheets and assets/blueprint/screen.css returns this:
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Action Controller: Exception caught</title>
I have checked that the path to the file is correct:
C:\triton2\public\stylesheets\blueprint\screen.css
And this is the top of my application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
<%= stylesheet_link_tag 'blueprint/screen.css', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print.css', :media => 'print' %>
</head>
Any idea what could be causing this? Better yet, how could I diagnose the proper solution on my own? Nothing on the google is of use save this which recommended a db:migrate (didn't help)
[edit]
The solution was to put the style sheet in C:\triton2\public\assets\stylesheets\blueprint\screen.css
and change application.html.erb to <%= stylesheet_link_tag 'stylesheets/blueprint/print.css', :media => 'print' %>
I might be wrong but I think it may need to be in the public/assets/stylesheets instead of just public/stylesheets if it is a rails 3.1 app.