If there a way to include the content of a javascript file into a Rails view?
I know about Ruby's File.read, but I'm searching for some helper already done.
<%= javascript_tag do %>
<%= render :file => '/path/to/rails/app/path/to/javascript.js' %>
<% end %>
So you can include any javascript code located on your machine. But to be honest there's more appropriate way to do that kind of things.
Related
I need to conditionally use the remotipart gem. The [docs][1] say just add it to application.js:
//= require jquery.remotipart
But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:
<%= javascript_include_tag "jquery.remotipart" %>
I get an error. How do i reference a js included as part of a gem generically, and remotipart js specifically?
Thanks,
Kevin
But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:
What means conditionally in this context? The most simplest way would be
<%= javascript_include_tag "jquery.remotipart" if condition %>
Also you could use content_for like this on the views where you want to include it:
# in your view
<% content_for :js do %>
<%= javascript_include_tag "jquery.remotipart" %>
<% end %>
# in your layout.html.erb
<%= yield :script %>
https://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
If you meant that you get an error that the JS file can't be found, please update your question with what library (webpacker, sprockets) and Rails version you use.
I'm creating an album that I'm planning to display with Masonry and ImgZoom so that the visitors can have a bigger image when they click on it.
According to ImgZoom, to make the zoom work, you need to do the following:
<a href="path/to/real/image.png">
<img src="path/to/image's/thumbnail.png class="thumbnail" />
</a>
So I generated an uploader, with the following inside it:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
'portfolio/photos'
end
version :thumb do
process :resize_to_fit => [220, nil]
end
end
Everything works perfectly, I can call both the versions without trouble, but when I try to follow ImgZoom's instructions by doing the following:
<%= #portfolio.photos.each do |p| %>
#This is a nested form inside the portfolio form, so I need to do this to get my images
<%= link_to image_tag p.image.thumb.url, p.image %>
or:
<%= link_to p.image do %>
<%= image_tag p.image.thumb.url, :class => 'thumbnail' %>
<% end %>
I'm getting the following error: undefined method 'to_model' for #<ImageUploader:0x0000000c35f4d8>
I found a similar subject on stack overflow but the asker wasn't clear and was invited to ask an other question on the forum, which I couldn't find.
I can individually reach 'p.image' and 'p.image.thumb.url', but I can't make a link from one to another, which would be perfectly doable with simple html.
What am I doing wrong?
Thank you in advance
First, to create the class "thumbnail" in the link, you need to declare it properly. I edited the link:
<%= link_to p.image.url do %>
<%= image_tag p.image.url, class: "thumbnail" %>
<% end %>
Second, you need to check if you created an appropriate route for viewing the image. This can be either done by linking to a static assets properly (as your image is not under "public") or via a template view.
If the files where stored under "public", your way of linking should work just fine.
Check out how image_path works in the docs: image_path (and more)
To those helping.. Thanks. Still no solution but getting closer. The errors i think are because my "file" evaluates to "app/views/main/show/_partial.html.erb". and render adds it's own relative path. So I guess i need to list the file names.. maybe a dir.foreach or dir.glob type command.. i'll keep digging. THANKS!
I have a directory full of partials I would like to call in a page. So I wrote a loop but it yields errors.
<% Dir["app/views/main/show/*"].each do |file| %>
<%= render #{file} %>
<% end %>
When I replace the render line with a simple file it lists the file names so i know the loop and Dir location work. The problem I THINK is that render is looking for a string. So I've tried all sorts of things from searching google and here like the #{file}, creating a variable first, raw,...etc.
also I think render may be looking in a different directory relative the Dir. I'll try out some stuff there to.
How should I be handling this? I'm up for switching from Dir to Dir.foreach or any other strategy that makes sense.
Thanks.
EDIT: Here's the solution I've implemented (directory path changed from above):
<% Dir["app/views/partials/show/*.html.erb"].each do |file| %>
<p> <%= render 'partials/show/' + File.basename(file,'.html.erb').slice!(1..-1) %></p>
<% end %>
file is already a string, so there is no need to escape. Also, you have to give the full path. This should work:
<% Dir["app/views/main/show/*"].each do |file| %>
<%= render :file => Rails.root.join(file).to_s %>
<% end %>
#{} only works inside double quotes. E.g. "#{file}", but in this case that's not necessary. You can just use file.
I am writing a Rails 3.2.1 application and I have some javascript code I'd like to put in for a single action view. It simply calls a jquery plugin and starts a countdown, but I'd like to write it in coffee script and I feel like the asset pipeline is the correct tool to do this.
Also I need access to the variables passed by the controller such as #question. How would I do this? I have looked into the coffeebeans gem but that only works for :remote=>true forms and links.
Your problem can be solved in different ways.
Add the assets dynamically
Add to our application helper the following method:
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 your layout-file:
<%= include_related_asset(params[:controller].to_param + '_' + params[:action].to_param . 'js') %>
Create specific assets for your controller actions. E. g. controller_action.js
Use yield
Add <%= yield :head%> to your layout head
Include your assets from your action views:
<% content_for :head do %>
<%= javascript_include_tag 'controller_action' %>
<% end %>
Please see the Rails guides for further information.
To passing controller data to your javascript, you could do:
<%= javascript_tag do %>
window.error_message = '<%= j error_message %>';
<% end %>
Please see the RailCast Episode #324 for further information.
I am in love with Netbeans Source->Format feature. Is there any way to get it to recognize stuff in these blocks
<% content_for :style do %>
<% end %>
as CSS? It would also help for autocomplete.
Ditto, of course, for
<% content_for :javascript do %>
<% end %>
blocks.
I'm not certain, but I think the source formatting is on a file basis only, not on parts of a file.
Edit: Actually, I'm wrong, the formatting can recognize various languages in the same file...obviously what's happening in an ERB file for ruby and html code. But I don't see a way of extending that.