It's inserting <%= %> into my HTML, but what is that for?
ASP-style short form tags for PHP scripts.
It's also used in ERB which is the ruby template engine and is used as the default template engine in Ruby on Rails.
ERB docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html
Rails use of ERB: http://api.rubyonrails.org/classes/ActionView/Base.html
Related
I am using Nokogiri to parse HTML files and am using EJS for my server-side templating.
I need to use Nokogiri to parse some EJS files, but the problem is that EJS uses syntax like <%= order_id %> as placeholders. This gets removed by Nokogiri which I am guessing is because it treats the file as malformed HTML.
Is there anyway around this?
I have a website running on middleman. I'm trying to check if an image file exists. I followed the suggestion in "How to check for file existence", using File.file?, but it's not working. Here's the line of code:
<% if File.exists?("/source/images/doctor-#{doctor.slug}.jpg") %>
I tried a few different file paths:
/source/images/file
/images/file
../images/file
but none worked. In the browser, I can view the image file at "0.0.0.0:4567/images/file".
Any ideas?
Maybe it's easier to query those files via the Middleman Sitemap?
You gain access to it within templates via the sitemap object. One way of achieving your actual case could be:
<% if sitemap.find_resource_by_path("images/doctor-#{doctor.slug}.jpg") %>
<%# do what you want %>
<% end %>
I'm working with a ruby website app and I'm not familiar with Ruby , I would like to know how you include say a nav template file in a html file, I'm familiar with how you would do this in PHP with require
So this would include the nav section on each page , what is the code to do this in Ruby
Many thanks
If you're using erb to generate your html, you have to add to the corresponding .erb file (in /app/views/*/):
<%= render 'nav' %>
And then the file *_nav.html.erb* will be included at that point (from the same dir where the includer file is).
I have recently came across where I would like to simply just include a coffeescript file in a single Rails view, but when you use the javascript_include_tag it appends .js to the end of the name you pass in (for obvious reasons).
Has anyone come up with a solution to simply just include a coffeescript file on a page without this happening?
i did this to load only the relevant JS for each controller:
in application.html.erb where you see
<%= javascript_include_tag "application" %>
please add
<%= javascript_include_tag controller_path, :media => "all" %>
also remove the
#=import .
from application.js
this way only the myController.coffee.js is loaded from myController this way you can have a more granular control of the loaded js and you can use application.js for common code
it's not the best solution in terms of user download but if you don't care about a few more connections I find this easier to understand (same thing can be done with css)
I have a rails 3.1 application that uses a default layout with css in "/app/assets/stylesheets/application.css" as per standard practice. This works great with the asset pipelining stuff for the main part of my application.
But I use a different "reporting" layout when I generate a report that uses a /app/views/layouts/showreports.html.erb layout as follows:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "showreports" %>
<%= javascript_include_tag "showreports" %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="mainarea" class="container">
<div class="span-24 last">
<%= yield %>
</div>
</div>
</body>
I'm using rake assets:clean; rake assets:precompile to pre-compile my assets and check things are looking good in the manifest file (/public/assets/manifest.yml) and I don't see any references to showreports.css or showreports.js.
When I test my program in dev-mode, unsurprisingly, it can't locate those files.
I'm guessing this is a basic sprockets question with rails 3.1 but I thought sprockets would be smart enough to parse all the layout files for assets (beyond the default application.html.erb file).
Just wondering if this may be a bug or just my misunderstanding on how this should work.
Cheers,
Greg
UPDATE
After more tinkering, I'm answering my own question as this made the most sense for me...
This answer really gave the right clue
What I was doing wrong was using files named "showreports.css" and "showreports.js" as manifest files.
My fix was to create /app/assets/stylesheets/showreports and /app/assets/javascripts/showreports and then to rename my showreports.(css|js) to application.(css|js) inside their respective directories.
After doing so, rake assets:precompile found them nicely and added them correctly to the manifest file.
According to http://guides.rubyonrails.org/asset_pipeline.html you should also add following:
config.assets.precompile += %w(showreports.css showreports.js)
to your application.rb file
It's a misunderstanding. Rails Assets Pipeline only compiles files located in RAILS_ROOT/app/assets and you must reference them directly. If you want another file to be generated simply create another manifest file with similar content to application.css/js. Also check require directives to avoid including one file into the other.